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
50
votes
4 answers

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 each customer can have multiple ORDERS, I want to…
Rob
  • 792
  • 2
  • 7
  • 15
50
votes
2 answers

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) def __unicode__(self): return…
eatonphil
  • 13,115
  • 27
  • 76
  • 133
49
votes
7 answers

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 why this is bad. It is my opinion that creating…
Erik Forbes
  • 35,357
  • 27
  • 98
  • 122
49
votes
5 answers

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 = "Enrollment Date")] public DateTime EnrollmentDate {…
Sonu K
  • 2,562
  • 2
  • 20
  • 32
47
votes
3 answers

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
  • 2,097
  • 2
  • 21
  • 26
46
votes
6 answers

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 layout as follows: …
Drew Hunter
  • 10,136
  • 2
  • 40
  • 49
46
votes
4 answers

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 got saved Eg: $myModel = new…
user2722667
  • 8,195
  • 14
  • 52
  • 100
46
votes
4 answers

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 = 20) def get_foo(self): if…
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
45
votes
16 answers

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 have specified: logon_model Now , I have done my…
Alexandre Bolduc
  • 805
  • 2
  • 10
  • 15
45
votes
3 answers

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 slightly unsure if I'm using the foreign key concept correctly. The insert I'm…
rahimbah
  • 599
  • 1
  • 4
  • 7
44
votes
4 answers

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
  • 1,495
  • 3
  • 16
  • 21
44
votes
7 answers

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 utility of model.fit() and model.predict(). But I am…
Abhijit Balaji
  • 1,870
  • 4
  • 17
  • 40
44
votes
3 answers

How to return an array instead of a collection in Laravel?

In Laravel is it possible to select only one field and return that as a set/ array. For example consider the model Foo which is linked to table foos which has field id, a, b, c. Consider the following sample data: (1, 10, 15, 20) (1, 12, 15, 27) (1,…
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
44
votes
3 answers

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 the instances of each model and then filter them.…
Nahanaeli Schelling
  • 1,235
  • 1
  • 12
  • 17
42
votes
4 answers

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 partial view, to allow for partial page updates…
MattSull
  • 5,514
  • 5
  • 46
  • 68