Questions tagged [models]

A model is an abstract representation of a real-life object or process. Models are a part of the popular MVC pattern, as well as a more general concept in the sciences for approximating behavior.

A model is one of the three components of Model-View-Controller (MVC) programming pattern that provides knowledge: data and how to work with this data, responding to requests by changing its state.

It can also refer to models in a more general scientific sense, denoting a representation (generally a simplification) of how a real-world process operates.

2397 questions
106
votes
4 answers

Create if doesn't exist

I have a Django application that reads data from a web API and puts it in a database. Is there a way to create a new object from a mode but prevent the duplicate exception if the object already exists? In other words, is there a way to save an…
user1094786
  • 6,402
  • 7
  • 29
  • 42
94
votes
3 answers

models.py getting huge, what is the best way to break it up?

Directions from my supervisor: "I want to avoid putting any logic in the models.py. From here on out, let's use that as only classes for accessing the database, and keep all logic in external classes that use the models classes, or wrap them." I…
Eddified
  • 3,085
  • 8
  • 36
  • 47
81
votes
1 answer

Rails 4: organize rails models in sub path without namespacing models?

Would it be possible to have something like this? app/models/ app/models/users/user.rb app/models/users/education.rb The goal is to organize the /app/models folder better, but without having to namespace the models. An unanswered question for Rails…
Rubytastic
  • 15,001
  • 18
  • 87
  • 175
78
votes
7 answers

Using multiple PostgreSQL schemas with Rails models

I have a PostgreSQL database for my Rails application. In the schema named 'public' the main Rails models tables are stored etc. I have created a 'discogs' schema which will have tables with names that are sometimes the same as in the 'public'…
Johan
  • 1,897
  • 5
  • 23
  • 29
57
votes
7 answers

Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model

I have very basic problem. In L4 thes below methods worked out of the box, so now I am lost. Please help. A few days ago I started a Laravel 5.0 project. I have now fresh, clean installation. Problem 1: When I try to get anything from…
Peter
  • 2,634
  • 7
  • 32
  • 46
57
votes
5 answers

Should I avoid multi-table (concrete) inheritance in Django by any means?

Many experienced developers recommend against using Django multi-table inheritance because of its poor performance: Django gotcha: concrete inheritance by Jacob Kaplan-Moss, a core contributor of Django. In nearly every case, abstract inheritance…
49
votes
6 answers

Create Ruby on Rails views (only) after controllers and models are already created

I've obtained a project that have controllers (minimal code only) and models, but the views are missing. Is there a way to generate the views only using scaffold or another tool?
Sean
  • 2,018
  • 4
  • 25
  • 32
48
votes
2 answers

How can I fill up form with model object data?

I want to fill up form with data from model instance. But my form has less fields than model. If I have code like this: class Item(models.Model) name = models.CharField(max_length=100) price = models.PositiveIntegerField() class…
krzyhub
  • 6,285
  • 11
  • 42
  • 68
43
votes
1 answer

AttributeError: 'ManyRelatedManager' object has no attribute 'add'? I do like in django website but got this error

for item in data: category_id = item['category_id'] del item['category_id'] category = Category.objects.get(pk=category_id) code = item['code'] try: article = Article.objects.get(pk=code) except: article =…
Totty.js
  • 15,563
  • 31
  • 103
  • 175
43
votes
6 answers

Ruby on Rails: Is it better to validate in the model or the database?

Is it generally better practice (and why) to validate attributes in the model or in the database definition? For (a trivial) example: In the user model: validates_presence_of :name versus in the migration: t.string :name, :null => false On the…
William Jones
  • 18,089
  • 17
  • 63
  • 98
42
votes
5 answers

guidelines for where to put classes in Rails apps that don't fit anywhere

I'm wondering if there are any best practices about where to put non-standard Ruby files in Rails apps, those that don't fit in any of the default directories (controllers/models etc.). I'm talking about classes that are used by controllers/models…
Kuba Suder
  • 7,587
  • 9
  • 36
  • 39
39
votes
2 answers

Could not find the association problem in Rails

I am fairly new to Ruby on Rails, and I clearly have an active record association problem, but I can't solve it on my own. Given the three model classes with their associations: # application_form.rb class ApplicationForm < ActiveRecord::Base …
Ash
  • 24,276
  • 34
  • 107
  • 152
34
votes
7 answers

Django model instances primary keys do not reset to 1 after all instances are deleted

I have been working on an offline version of my Django web app and have frequently deleted model instances for a certain ModelX. I have done this from the admin page and have experienced no issues. The model only has two fields: name and order and…
pj2452
  • 905
  • 3
  • 10
  • 22
33
votes
6 answers

Rails: how to require at least one field not to be blank

I know I can require a field by adding validates_presence_of :field to the model. However, how do I require at least one field to be mandatory, while not requiring any particular field? thanks in advance -- Deb
deb
  • 12,326
  • 21
  • 67
  • 86
31
votes
3 answers

Better option to check if a particular instance exists django

Which of the two is a better and efficient option to check if an instance exists. There can only be one record returned at most. 1) Use the filter option and see if it exists: x = MyObject.objects.filter(someField=someValue).count() if x: …
Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69
1
2 3
99 100