Questions tagged [model]

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

The MVC (Model, View, Controller) pattern separates objects into one of three categories — models for maintaining data, views for displaying all or a portion of the data, and controllers for handling events that affect the model or view(s).

Frameworks such as Laravel, Rails, Django and ASP.NET MVC apply this pattern in the web development domain.

18996 questions
58
votes
4 answers

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 to initialize all the variables one by one and it…
Bhavuk Jain
  • 2,167
  • 1
  • 15
  • 23
58
votes
9 answers

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.Model): name =…
vagabond
  • 581
  • 1
  • 4
  • 4
57
votes
6 answers

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 further that I wish the rows in the DB contain…
dfrankow
  • 20,191
  • 41
  • 152
  • 214
57
votes
1 answer

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 sql myapp the sql statement always end up…
Robin
  • 9,415
  • 3
  • 34
  • 45
55
votes
4 answers

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() then do I also need to call CreateMap
BlueChippy
  • 5,935
  • 16
  • 81
  • 131
55
votes
14 answers

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

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 where method:
Osman Goni Nahid
  • 1,193
  • 2
  • 15
  • 24
54
votes
1 answer

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 associated with the objects and I am trying to…
ccred
  • 543
  • 4
  • 5
53
votes
2 answers

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: owner: "Владелец" And this: activerecord: …
Alex
  • 2,309
  • 2
  • 16
  • 20
53
votes
8 answers

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/migrations But what I can't find is how to delete a…
Jonathan Solorzano
  • 6,812
  • 20
  • 70
  • 131
53
votes
9 answers

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 JSP (I'm using JSF). Or convert a domain model…
sma
  • 9,449
  • 8
  • 51
  • 80
53
votes
5 answers

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 particular field's default value from a model?
mikec
  • 3,543
  • 7
  • 30
  • 34
52
votes
7 answers

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 to generate a Swift model from Core Data entity: On…
Jean Lebrument
  • 5,079
  • 8
  • 33
  • 67
51
votes
8 answers

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, but obviously I can't repeat the :children in a…
Kenzie
  • 1,285
  • 2
  • 11
  • 13
51
votes
3 answers

Local functions in Python

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 recognise that a is an immutable value in this context,…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
51
votes
3 answers

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: validates_presence_of :password, :on => [ :create, :update ] Do I…
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327