Questions tagged [model]

Part of the MVC pattern, the Model manages the behaviour and data of the application.

Filter by
Sorted by
Tagged with
73 votes
5 answers
40k views

Should I make a DateRange object?

A few of my domain objects contain date ranges as a pair of start and end date properties: public class Period { public DateTime EffectiveDate { get; set; } public DateTime ThroughDate { get; set;...
quentin-starin's user avatar
72 votes
3 answers
23k views

Where Should Model State Be Stored In Angular.js

I'm finding Angular's use of models confusing. Angular seems to take the approach that a model can be anything you like - I.E. Angular does not include an explicit model class and you can use vanilla ...
Undistraction's user avatar
70 votes
5 answers
33k views

DDD and MVC: Difference between 'Model' and 'Entity'

I'm seriously confused about the concept of the 'Model' in MVC. Most frameworks that exist today put the Model between the Controller and the database, and the Model almost acts like a database ...
Nathan Loding's user avatar
68 votes
5 answers
88k views

Populating django field with pre_save()?

class TodoList(models.Model): title = models.CharField(maxlength=100) slug = models.SlugField(maxlength=100) def save(self): self.slug = title super(TodoList, self).save() ...
Derek's user avatar
  • 12.2k
68 votes
3 answers
26k views

How to know whether a model is new or not?

class Post < ActiveRecord::Base end post = Post.new How do I judge whether the 'post' is a new model which is not pulled from the database?
Croplio's user avatar
  • 3,503
67 votes
9 answers
91k views

How I can put composite keys in models in Laravel 5?

I have in my DataBase a table with two primary keys (id and language_id) and I need put it in my models. The default primaryKey in Models (Model.php in Laravel 5) is id, and I want that the ...
Sergioh Lonet's user avatar
66 votes
8 answers
52k views

Rails after_initialize only on "new"

I have the following 2 models class Sport < ActiveRecord::Base has_many :charts, order: "sortWeight ASC" has_one :product, :as => :productable accepts_nested_attributes_for :product, :...
Tyler DeWitt's user avatar
  • 23.5k
65 votes
2 answers
70k views

When to use Class or Interface in Angular project? [closed]

I’m here today because I’ve a question about, like the title said, about classes and interfaces in Angular. From my Point of View, I understand this: Interfaces are used in Typescript to perform ...
Scieur Arnaud's user avatar
65 votes
4 answers
122k views

Email model validation with DataAnnotations and DataType

I have following model: public class FormularModel { [Required] public string Position { get; set; } [Required] [DataType(DataType.EmailAddress)] public string Email { get; set; } ...
lifeofbenschi's user avatar
65 votes
3 answers
44k views

How can I iterate over ManyToManyField?

A simple question and yet I can't find an answer for it. I have a model with a ManyToMany field: class Stuff(models.Model): things = models.ManyToManyField(Thing) then in a different function I ...
Goro's user avatar
  • 10.1k
65 votes
1 answer
14k views

Is it bad practice to include non-validating methods in a pydantic model?

I'm using pydantic 1.3 to validate models for an API I am writing. Is it common/good practice to include arbitrary methods in a class that inherits from pydantic.BaseModel? I need some helper methods ...
ccred's user avatar
  • 653
64 votes
11 answers
22k views

Custom tag helper not working

I followed a few guides on creating a custom tag helper for ASP Core. This is my helper: using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Razor.TagHelpers; using System; ...
Matthew Goulart's user avatar
61 votes
14 answers
72k views

PHPStorm is not recognizing methods of my Model class in Laravel 5.0 [duplicate]

failed insert data into database, and all query class and Model class's method not found show in IDE (phpStrom) how can I solve it? here is my extended class (Post.php) here show error in latest and ...
Osman Goni Nahid's user avatar
60 votes
9 answers
35k views

Can Django automatically create a related one-to-one model?

I have two models in different apps: ModelA and ModelB. They have a one-to-one relationship. Is there a way django can automatically create and save ModelB when ModelA is saved? class ModelA(models....
vagabond's user avatar
  • 601
59 votes
6 answers
65k views

Rails has_many with dynamic conditions

What I want is to create a Model that connects with another using a has_many association in a dynamic way, without the foreign key like this: has_many :faixas_aliquotas, :class_name => 'Fiscal::...
Fabiano Soriani's user avatar
59 votes
4 answers
29k views

How to automatically create an initializer for a Swift class?

UPDATE: Use structs and not classes. Struct is better in many ways has got an initializer of its own. This is my model class. Is it possible to create the init method automatically? Everytime I have ...
Bhavuk Jain's user avatar
  • 2,177
59 votes
5 answers
92k views

How to set a default attribute value for a Laravel / Eloquent model?

If I try declaring a property, like this: public $quantity = 9; ...it doesn't work, because it is not considered an "attribute", but merely a property of the model class. Not only this, but also I ...
J. Bruni's user avatar
  • 20.4k
58 votes
3 answers
78k views

Ruby on Rails: errors.add_to_base vs. errors.add

I have read that errors.add_to_base should be used for errors associated with the object and not a specific attribute. I am having trouble conceptualizing what this means. Could someone provide an ...
Tony's user avatar
  • 18.9k
58 votes
1 answer
57k views

Django model: NULLable field

I'm working on a django project where I need a DateField to sometimes be empty. My model looks like this: #models.py end = models.DateField(default=None, blank=True) But when I run python manage.py ...
Robin's user avatar
  • 9,555
57 votes
6 answers
38k views

How do you validate uniqueness of a pair of ids in Ruby on Rails?

Suppose the following DB migration in Ruby: create_table :question_votes do |t| t.integer :user_id t.integer :question_id t.integer :vote t.timestamps end Suppose ...
dfrankow's user avatar
  • 20.6k
57 votes
7 answers
185k views

Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model

I have very basic problem. In L4 thes below methods worked out of the box, so now I am lost. Please help. A few days ago I started a Laravel 5.0 project. I have now fresh, clean installation. Problem ...
Peter's user avatar
  • 2,692
57 votes
5 answers
14k views

Should I avoid multi-table (concrete) inheritance in Django by any means?

Many experienced developers recommend against using Django multi-table inheritance because of its poor performance: Django gotcha: concrete inheritance by Jacob Kaplan-Moss, a core contributor of ...
utapyngo's user avatar
  • 7,006
57 votes
8 answers
132k views

How to delete a model using php artisan?

Is there a command to safely delete a model in Laravel 5? To create a model we use php artisan make:model modelname And that will create a model under app folder, and also a migration in database/...
Jonathan Solorzano's user avatar
55 votes
4 answers
20k views

Do i need to create automapper createmap both ways?

This might be a stupid question! (n00b to AutoMapper and time-short!) I want to use AutoMapper to map from EF4 entities to ViewModel classes. 1) If I call CreateMap<ModelClass, ViewModelClass>...
BlueChippy's user avatar
  • 6,021
54 votes
5 answers
18k views

How do I get the default value for a field in a Django model?

I have a Django model with some fields that have default values specified. I am looking to grab the default value for one of these fields for us later on in my code. Is there an easy way to grab a ...
mikec's user avatar
  • 3,623
53 votes
2 answers
10k views

How to translate model in Ruby class/module namespace?

I have a model Products::Car. How can I translate its attributes? I have already tried this: activerecord: models: products: car: "Автомобиль" attributes: products: car: ...
Jack's user avatar
  • 2,329
53 votes
9 answers
28k views

DTO or Domain Model Object in the View Layer?

I know this is probably an age-old question, but what is the better practice? Using a domain model object throughout all layers of your application, and even binding values directly to them on the ...
sma's user avatar
  • 9,537
52 votes
7 answers
46k views

Generating Swift models from Core Data entities

Update for Xcode 8: In Xcode 8, one needs to go to the Core Data Model Editor and Show the File Inspector. Near the bottom is an option for code generation. Select Swift. Edit: I found the solution ...
Jean Lebrument's user avatar
51 votes
5 answers
42k views

How to add boolean required attribute in mvc?

I have one model class like: public class Student { [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] [Display(Name = "...
Sonu K's user avatar
  • 2,652
51 votes
8 answers
25k views

Rails Model has_many with multiple foreign_keys

Relatively new to rails and trying to model a very simple family "tree" with a single Person model that has a name, gender, father_id and mother_id (2 parents). Below is basically what I want to do, ...
Kenzie's user avatar
  • 1,285
51 votes
3 answers
58k views

Local functions in Python [duplicate]

In the following Python code, I get an UnboundLocalError. As I understand it, local functions share the local variables of the containing function, but this hardly seems to be the case here. I ...
Matt Joiner's user avatar
51 votes
3 answers
51k views

Rails model validation on create and update only

If I want to have validation only on create, then I can do validates_presence_of :password, :on => :create But how do I say on create and update? I tried this but it didn't work: ...
Jakub Arnold's user avatar
  • 86.4k
50 votes
4 answers
20k views

How do I prevent deletion of parent if it has child records?

I have looked through the Ruby on Rails guides and I can't seem to figure out how to prevent someone from deleting a Parent record if it has Children. For example. If my database has CUSTOMERS and ...
Rob's user avatar
  • 780
50 votes
2 answers
55k views

optional fields in django models

I have the following model in django. class Link(models.Model): name = models.CharField(max_length=100) url = models.CharField(max_length=100) tag = models.CharField(max_length=100) ...
eatonphil's user avatar
  • 13.4k
50 votes
6 answers
28k views

Create Ruby on Rails views (only) after controllers and models are already created

I've obtained a project that have controllers (minimal code only) and models, but the views are missing. Is there a way to generate the views only using scaffold or another tool?
Sean's user avatar
  • 2,038
49 votes
7 answers
12k views

MVC: Data Models and View Models

I've read some MVC advice in the past regarding models stating that you should not reuse the same model objects for the domain and the view; but I haven't been able to find anyone willing to discuss ...
Erik Forbes's user avatar
  • 35.7k
48 votes
3 answers
111k views

Create Models from database in Laravel

Is there some way to generate models from database in Laravel? The generators package only create an empty model.
Santi Barbat's user avatar
  • 2,247
48 votes
2 answers
43k views

How can I fill up form with model object data?

I want to fill up form with data from model instance. But my form has less fields than model. If I have code like this: class Item(models.Model) name = models.CharField(max_length=100) price =...
krzyhub's user avatar
  • 6,375
47 votes
16 answers
187k views

CodeIgniter PHP Model Access "Unable to locate the model you have specified"

I have been trying to load some models for this website I am building. However, for an unknown reason, it will bring the following error : An Error Was Encountered Unable to locate the model you ...
Alexandre Bolduc's user avatar
47 votes
4 answers
107k views

Check if laravel model got saved or query got executed

I've seen alot of people using this way to check if a laravel model got saved. So now I wonder if it is a safe way. And also can I check if the queries bellow got executed like this Check if model ...
user2722667's user avatar
  • 8,495
47 votes
3 answers
78k views

ValueError - Cannot assign: must be an instance

I get the following error when I try to run an insert into one of my tables. Cannot assign "1": "Team.department_id" must be a "Department" instance Admittedly I'm ...
rahimbah's user avatar
  • 629
46 votes
6 answers
55k views

Magento - Passing data between a controller and a block

Really quick and simple question but I can't find a decent answer to this - What is the best way to pass data from a controller to a block in Magento. Incase it makes a difference, I am loading the ...
Drew Hunter's user avatar
  • 10.1k
46 votes
4 answers
47k views

Do properties work on Django model fields?

I think the best way to ask this question is with some code... can I do this: class MyModel(models.Model): foo = models.CharField(max_length = 20) bar = models.CharField(max_length = ...
Jiaaro's user avatar
  • 75.7k
44 votes
4 answers
138k views

MVC 4 - how do I pass model data to a partial view?

I'm building a profile page that will have a number of sections that relate to a particular model (Tenant) - AboutMe, MyPreferences - those kind of things. Each one of those sections is going to be a ...
MattSull's user avatar
  • 5,554
44 votes
4 answers
59k views

How does one add an attribute to a model?

In rails I generate a model with two strings and would like to add more. How would I go about doing this?
jsttn's user avatar
  • 1,505
44 votes
3 answers
50k views

Django Pass Multiple Models to one Template

I am building an address book that includes the relationships between entries, etc. I have separate models for Individuals, Companies, Venues, and Roles. On my index page I would like to list all of ...
Nahanaeli Schelling's user avatar
44 votes
7 answers
73k views

What is the difference between model.fit() an model.evaluate() in Keras?

I am using Keras with TensorFlow backend to train CNN models. What is the between model.fit() and model.evaluate()? Which one should I ideally use? (I am using model.fit() as of now). I know the ...
Abhijit Balaji's user avatar
43 votes
3 answers
72k views

Restrict django FloatField to 2 decimal places

I am looking for a way to limit the FloatField in Django to 2 decimal places. Has anyone got a clue of how this could be done without having to use a DecimalField? I tried decimal_places=2, but this ...
Aidan Doherty's user avatar
43 votes
6 answers
75k views

How can I change a Django form field value before saving?

if request.method == 'POST': userf = UsersModelForm(request.POST) username = userf.data['username'] password = userf.data['password'] passwordrepeat = userf.data['passwordrepeat'] ...
cola's user avatar
  • 12.3k
43 votes
1 answer
33k views

Instance the query builder directly from model

When I do something like SomeModel::with('user') it returns a Query\Builder instance. How can I get this instance without need call the with() (or similar)? For instance, I tried it: new SomeModel, ...
David Rodrigues's user avatar