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

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; } } public class Timeline { public DateTime…
quentin-starin
  • 26,121
  • 7
  • 68
  • 86
72
votes
3 answers

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 JavaScript objects as models. In almost every…
Undistraction
  • 42,754
  • 56
  • 195
  • 331
69
votes
5 answers

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 abstraction layer. The concept of 'Fat Model Skinny…
Nathan Loding
  • 3,185
  • 2
  • 37
  • 43
68
votes
9 answers

Case insensitive unique model fields in Django?

I have basically a username is unique (case insensitive), but the case matters when displaying as provided by the user. I have the following requirements: field is CharField compatible field is unique, but case insensitive field needs to be…
noobzie
  • 1,049
  • 3
  • 12
  • 16
68
votes
5 answers

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() I'm assuming the above is how to create and…
Derek
  • 11,980
  • 26
  • 103
  • 162
67
votes
3 answers

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
  • 3,433
  • 6
  • 31
  • 37
67
votes
9 answers

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 primaryKeys will be id and id_language. I tried put it with…
Sergioh Lonet
  • 831
  • 1
  • 7
  • 9
66
votes
8 answers

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, :allow_destroy => true end class Product <…
Tyler DeWitt
  • 23,366
  • 38
  • 119
  • 196
65
votes
5 answers

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; } [Required] public string Webcode { get;…
lifeofbenschi
  • 777
  • 1
  • 6
  • 12
62
votes
3 answers

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 want to do this: myStuff =…
Goro
  • 9,919
  • 22
  • 74
  • 108
61
votes
2 answers

When to use Class or Interface in Angular project?

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 type-checking, they are here until the transpilation…
Scieur Arnaud
  • 823
  • 2
  • 7
  • 13
59
votes
11 answers

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; namespace ToolControlSystem.TagHelpers { …
Matthew Goulart
  • 2,873
  • 4
  • 28
  • 63
59
votes
6 answers

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::FaixaAliquota', :conditions =>…
Fabiano Soriani
  • 8,182
  • 9
  • 43
  • 59
59
votes
6 answers

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 am blocking access to the actually real and…
J. Bruni
  • 20,322
  • 12
  • 75
  • 92
58
votes
3 answers

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 example of when I would want to use each? For…
Tony
  • 18,776
  • 31
  • 129
  • 193