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
42
votes
3 answers

Call a model method in a Controller

I'm have some difficulties here, I am unable to successfully call a method which belongs to a ProjectPage model in the ProjectPage controller. I have in my ProjectPage controller: def index @searches = Project.published.financed …
sidney
  • 2,704
  • 3
  • 28
  • 44
41
votes
6 answers

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'] email = userf.data['email'] I tried this: …
cola
  • 12,198
  • 36
  • 105
  • 165
41
votes
4 answers

How to set a hidden value in Razor

I know that what I'm trying to do is bad idea, but I have specific constrains for now. I have multiple sites, using one and the same MVC3 code base. For one of them the requirement is to hide some required fields from the form. I know that the best…
Sunny Milenov
  • 21,990
  • 6
  • 80
  • 106
41
votes
1 answer

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, but it'll returns obviously the instance of my…
David Rodrigues
  • 12,041
  • 16
  • 62
  • 90
41
votes
6 answers

Rails Model without a table

I want to create a select list for lets say colors, but dont want to create a table for the colors. I have seen it anywhere, but can't find it on google. My question is: How can I put the colors in a model without a database table? Or is there a…
user993460
  • 821
  • 2
  • 8
  • 8
40
votes
3 answers

Rails Internationalization (I18n) in model validations: Possible or not?

I have the following validation in a model: validates_inclusion_of :whatever, :in => [true, false], :message => I18n.t('please_select_whatever') It seems that the translation does not work in production mode: in all languages it's always the…
40
votes
3 answers

How to get the app a Django model is from?

I have a model with a generic relation: TrackedItem --- genericrelation ---> any model I would like to be able to generically get, from the initial model, the tracked item. I should be able to do it on any model without modifying it. To do that I…
Bite code
  • 578,959
  • 113
  • 301
  • 329
40
votes
6 answers

rails model has_many of itself

I have a event model. Events can have parent events, set from a column in the model (parent_event_id). I need to be able to do has_many :event on the model, so I can just do, for example, event.child_event or event.parent_event. But my googling…
Alexander Forbes-Reed
  • 2,823
  • 4
  • 27
  • 44
40
votes
4 answers

How to implement Active Record inheritance in Ruby on Rails?

How to implement inheritance with active records? For example, I want a class Animal, class Dog, and class Cat. How would the model and the database table mapping be?
andrisetiawan
  • 461
  • 2
  • 5
  • 5
39
votes
6 answers

Rails Models: how would you create a pre-defined set of attributes?

I'm trying to figure out the best way to design a rails model. For purposes of the example, let's say I'm building a database of characters, which may have several different fixed attributes. For instance: Character - Morality (may be "Good" or…
Andrew
  • 42,517
  • 51
  • 181
  • 281
39
votes
3 answers

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 just gave me a migration error within the float…
Aidan Doherty
  • 962
  • 2
  • 11
  • 29
38
votes
4 answers

Tensorflow: How to convert .meta, .data and .index model files into one graph.pb file

In tensorflow the training from the scratch produced following 6 files: events.out.tfevents.1503494436.06L7-BRM738 model.ckpt-22480.meta checkpoint model.ckpt-22480.data-00000-of-00001 model.ckpt-22480.index graph.pbtxt I would like to…
Rafal
  • 479
  • 1
  • 4
  • 10
38
votes
3 answers

Usage of MVVM in iOS

I'm an iOS developer and I'm guilty of having Massive View Controllers in my projects so I've been searching for a better way to structure my projects and came across the MVVM (Model-View-ViewModel) architecture. I've been reading a lot of MVVM with…
Isuru
  • 30,617
  • 60
  • 187
  • 303
38
votes
5 answers

Check if a OneToOne relation exists in Django

Now I'm using django 1.6 I have two models relates with a OneToOneField. class A(models.Model): pass class B(models.Model): ref_a = models.OneToOneField(related_name='ref_b', null=True) First see my code that points out the problem: a1 =…
Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
37
votes
1 answer

model.matrix generates fewer rows than original data.frame

Why doesn't a model matrix necessarily have the same number of rows as the data frame? mergem = model.matrix(as.formula(paste(response, '~ .')), data=mergef) dim(mergef) # [1] 115562 71 dim(mergem) # [1] 66786 973 I tried looking for hints in…
Yang
  • 16,037
  • 15
  • 100
  • 142