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
93
votes
5 answers

DisplayNameFor() From List in Model
I believe this is pretty simple, I just can't seem to find the right way to show the display name for an item within a list within my model. My simplified model: public class PersonViewModel { public long ID { get; set; } private…
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
92
votes
10 answers

Using ActiveRecord, is there a way to get the old values of a record during after_update

Setup using a simple example: I've got 1 table (Totals) that holds the sum of the amount column of each record in a second table (Things). When a thing.amount gets updated, I'd like to simply add the difference between the old value and the new…
Abel
  • 2,115
  • 3
  • 18
  • 24
90
votes
5 answers

Django Queryset with filtering on reverse foreign key

I have the following Django model: class Make: name = models.CharField(max_length=200) class MakeContent: make = models.ForeignKey(Make) published = models.BooleanField() I'd like to know if it's possible (without writing SQL directly)…
Julian A.
  • 10,928
  • 16
  • 67
  • 107
88
votes
2 answers

Reusing a Model Built in R

When building a model in R, how do you save the model specifications such that you can reuse it on new data? Let's say I build a logistic regression on historical data but won't have new observations until next month. What's the best…
Btibert3
  • 38,798
  • 44
  • 129
  • 168
84
votes
8 answers

Django get list of models in application

So, i have a file models.py in MyApp folder: from django.db import models class Model_One(models.Model): ... class Model_Two(models.Model): ... ... It can be about 10-15 classes. How to find all models in the MyApp and get their names?…
Feanor
  • 3,568
  • 5
  • 29
  • 49
84
votes
4 answers

Differentiating between domain, model, and entity with respect to MVC

Can someone explain these 3 concepts and the differences between them with respect to an MVC framework along with an example. To me these appear almost equivalent, and it seems they are used interchangeably in some articles and not in others.
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
81
votes
5 answers

How to access URL helper from rails module

I have a module with a function. It resides in /lib/contact.rb: module Contact class << self def run(current_user) ... end end end I want to access the URL helpers like 'users_path' inside the module. How do I do that?
sizzle
  • 2,222
  • 2
  • 21
  • 32
79
votes
2 answers

Django - Model graphic representation (ERD)

I'm searching a way to represent my Django project model graphically. Is there a "native" way to do this kind of ERD (diagram) ? Update following @Etienne instructions Here is an example of how I finally view the PDF representing some models of my…
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
78
votes
4 answers

DDD - Persistence Model and Domain Model

I am trying to learn domain-driven design (DDD), and I think I got the basic idea. But there is something confusing me. In DDD, are the persistence model and domain model different things? I mean, we design our domain and classes with only domain…
ayk
  • 1,407
  • 2
  • 19
  • 26
78
votes
6 answers

Django required field in model form

I have a form where a couple of fields are coming out as required when I don't want them too. Here is the form from models.py class CircuitForm(ModelForm): class Meta: model = Circuit exclude = ('lastPaged',) def…
Ryan
  • 1,085
  • 1
  • 10
  • 15
76
votes
2 answers

Fat models, skinny controllers and the MVC design pattern

I just read a blog post that explains MVC with a banking analogy. I have a few months of experience with web application development with an MVC framework (CakePHP), so I get the basics, but I began to see a theme that made me think I'm taking a…
ryonlife
  • 6,563
  • 14
  • 51
  • 64
76
votes
11 answers

All Levels of a Factor in a Model Matrix in R

I have a data.frame consisting of numeric and factor variables as seen below. testFrame <- data.frame(First=sample(1:10, 20, replace=T), Second=sample(1:20, 20, replace=T), Third=sample(1:10, 20, replace=T), …
Jared
  • 3,510
  • 3
  • 25
  • 28
76
votes
4 answers

ForeignKey does not allow null values

I am using the Django REST Framework 2.0. Here is my model class: class Mission(models.Model): assigned_to = models.ForeignKey('auth.User', related_name='missions_assigned', …
Benjamin Toueg
  • 10,511
  • 7
  • 48
  • 79
75
votes
16 answers

Rails before_validation strip whitespace best practices

I would like my User model to sanitize some input before before save. For now some simple whitespace stripping will do. So to avoid people registering with "Harry " and pretend to be "Harry", for example. I assume it is a good idea to do this…
berkes
  • 26,996
  • 27
  • 115
  • 206
74
votes
4 answers

How can I set two primary key fields for my models in Django?

I have a model like this: class Hop(models.Model): migration = models.ForeignKey('Migration') host = models.ForeignKey(User, related_name='host_set') How can I have the primary key be the combination of migration and host?
Vahid Kharazi
  • 5,723
  • 17
  • 60
  • 103