I know this is a very basic question. I am learning django and i see the most important part is ForeignKey field and ManyToManyField. They are used ubiquitously. Without understanding those two, a proper model cannot be designed. If i have to design a model with FK relation, i always have to see the example first and try to come with the solution. I cannot confidently design a model cause i have not understand this well. It would be great if someone make me understand so that the picture comes to my head what is FKField, how FKField and MTMField are generated in table with simple english(Language is one of the barrier for me to understand from the documentation).
Here is the model for foreign key
class Category(models.Model):
name = models.CharField()
class Product(models.Model):
name = models.CharField()
category = models.ForeignKeyField(Category, related_name="product")
In django, you can add one instance of a "variable" as a part of a table: That is a ForeignKey.
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=30)
category = models.ForeignKey(Category)
class Category(models.Model):
name = models.CharField(max_length=30)
Here, you will have a SQL table named "[NAME OF YOUR APP]_product" that will have two columns: "name" and "category_id".
You will have an other table named "[NAME OF YOUR APP]_category" that will contain one column "name".
Django will know that when you load a Product, it will have to get its category_id, and then get that element from the category table.
This is because you use a foreignkey: it is one "variable". And it is "Many to One" because you can have many Products having the same Category.
Then you have "Many to Many". Here you can have more than one "variable"
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=30)
category = models.ManyToManyField(Category)
class Category(models.Model):
name = models.CharField(max_length=30)
Here, the difference is that you will get a table named "[NAME OF YOUR APP]_product" with only one column: "name".
Next to that, you will have a table "[NAME OF YOUR APP]_product_category", that will have the columns "product_id" and "category_id".
And one last table that will be "[NAME OF YOUR APP]_category" that will have one column: "name".
The way it works is that Django will get the Product, and see that it have a ManyToMany field of Category.
It will go to "[NAME OF YOUR APP]_product_category" and get the list of ids for the product_id you need, and get them from "[NAME OF YOUR APP]_category".
This is Many to Many because you can have a lot of Products that have each lots of different Category.
If you still don't understand, I will edit this post to add a SQL example of what the database looks like.
(Sorry, this is not really pleasant to read and a really broad way to explain how Django handle things, but I tried to do short and simple statements.)
Related
I need an OneToMany-Relationship because I want to store Recipes. To do that I have two Models one is the Recipe and the other is the ingredient. When googling for this topic I always found using a foreign-key but I am not sure if its that what I am looking for. I wanted to test it but I found nowhere how to use this relationship.
The Models:
class Ingredient(models.Model):
ingredient_title = models.CharField(max_length=120)
amount = models.IntegerField()
unit = models.CharField(max_length= 10)
class Recipe(models.Model):
title = models.CharField(max_length=120)
ingredients = models.ForeignKey(Ingredient,on_delete=models.CASCADE) `#Here I am not sure if its right`
preperation = models.TextField(default='Here comes the preperation...')
I tried creating a recipe model but on the admin page I could select just one ingredient and in the shell, I didn't know how to do that.
Here is what I tried:
Recipe.objects.create(title='Essen1',ingredients=[(ingredient_title="ZutatTitel1",amount=2,unit='g'),(ingredient_title="ZutatTitel1",amount=2,unit='g')],preperation='prep1'))
you need to use ManytoMany Field. A recipe can have many ingredients.
class Recipe(models.Model):
title = models.CharField(max_length=120)
ingredients = models.ManyToManyField(Ingredient)
preperation = models.TextField(default='Here comes the preperation...')
recipe_obj = Recipe.objects.create(title='Essen1)
recipe_obj.ingredients.add(ingredient_obj1)
recipe_obj.ingredients.add(ingredient_obj2)
As Neeraj said you need ManyToManyField instead of ForeignKey. This is because one ingredient can have (or belong to) many recipes and one recipe can have many ingredients. ForeignKey is used for many-to-one relationships - for example, one author might have many books but if each book has only one author then it would be a many-to-one relationship so ForeignKey. If however each book also had many authors then it would be a many-to-many relationship (one book has many authors and one author has many books).
I am trying to make an application for a restaurant in django; I have to create a menu for different types of items, and all of those different types of items have to essentially be a product, so that I can add that product to a user's corresponding cart. Here are my menu items:
from django.db import models
from django.contrib.auth.models import User
class Product(models.Model):
price = models.DecimalField(decimal_places=2, max_digits=10)
class Pizza(Product):
pizzatype = models.CharField(max_length=15)
extras = models.TextField(max_length=50)
size = models.CharField(max_length=10)
class Subs(Product):
name = models.TextField(max_length=64)
size = models.CharField(max_length=10)
class DinnerPlatters(Product):
name = models.TextField(max_length=64)
size = models.CharField(max_length=10)
class Pasta(Product):
name = models.TextField(max_length=64)
class Salads(Product):
name = models.TextField(max_length=64)
As can be seen, I tried deriving the models for different types of menu items from a single model Product, but while running makemigrations, I get the following message on the terminal:
You are trying to add a non-nullable field 'product_ptr' to dinnerplatters without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
I tried googling the solution to my problem, but couldn't find an approriate solution.
I am new to Django and this is my first application, so any help or any other method on how to approach my problem would be kindly appreciated.
As your models inherit from the Product's model, you are using Multi-table inheritance. Now that Multi-table inheritance automatically creates OneToOneField as classname_ptr between inheritance model and base model, your models which is inherit from product's model has product_ptr field. As your model has product_ptr field, you should add default value for existing rows which is save before by you.
https://docs.djangoproject.com/en/3.0/topics/db/models/#multi-table-inheritance
I have two models: Category and Item. Item can have 2 or more categories so I need to have a relationship on categories(maybe one-to-many) from the item I create. But, I also need to fetch that items related to a category(or categories), maybe like this:
http://example.com/api/items?category_id=5
Can anyone advice how can I achieve that? Thanks.
I believe, you have manytomany relation between Item and Category as
class Category:
.....
class Item:
category = ManytoMany relation to Category
In this case, you can extract Item related to category as following:
Suppose you have Category object as c1
c1.item_set.all()
Here c1 = Category.objects.get(id=5)
Because an item may be in multiple categories and, also, a category may have multiple items between Category and Item models must be a Many-to-Many relationship which you can define as follows:
class Category(models.Model):
name = models.CharField(max_length=30)
.....
and
class item(models.Model):
....
categories = modles.ManyToManyField(Category)
In the docs you can see more details of operations that can be performed using the Python API facilities.
The best practice I would suggest is, add many-to-many relation of category table in your item table.
For example:
class Category(models.Model):
# Write here your fields name
class Item(models.Model):
categories = models.ManyToManyField(Category)
I have to create a STudent Management module by Odoo 10.0 in windows. So I'd like to know exactly how to establish a "many2many" relation in Odoo (not Openerp). I have searched a lot of solutions on the Internet, but I don't understand clearly them :
enter image description here
For example, there are 2 classes (Student and Course) following with their attributes :
class Student(models.Model):
_name = "management.student"
IdStudent = fields.Integer() #primary key
Name = fields.Char()
Gender = fields.Char()
Address = fields.Char()
class Course(models.Model):
_name = "management.course"
IdCourse = fields.Integer() #primary key
course = fields.Char()
credit = fields.Integer()
professor = fields.Char()
Thanks a lot for your help !
Many2many relations in Odoo are best described as many records of one model can be linked to many records of another model. To use your example many classes can have many students and students can have many classes, hence many2many. As apposed to many2one such as an apple can only have only one tree or one2many one tree can have many apples.
To define a many2many relationship for Courses and Students you can create a field on the course like this. (taken from the docs)
attendee_ids = fields.Many2many('management.student', string="Attendees")
In this case because you have not specified the optional arguments column1,column2 Odoo will create a new relation table linking the two models. The table will have a name like this.
management_course_management_student_rel
However you can specify your own table name and columns for your relation table.
attendee_ids = fields.Many2many('management.student',relation='your_table_name', column1='course_id',column2='student_id', string="Attendees")
This would produce a table called your_table_name with two columns course_id and student_id
To determine what students are in a course Odoo would execute a query
SELECT student_id from your_table_name where course_id = x
And the opposite to find what courses a student is taking.
I need to make a smart menu, for which I need a ManyToMany relation.
My model is:
from django.db import models
class Health_plan(models.Model):
a = models.IntegerField ()
b = models.IntegerField ()
class Doctors_list(models.Model):
name = models.CharField(max_length=30)
hp_id = models.ManyToManyField(Health_plan)
def __unicode__(self):
return self.name
How do I make this relation in the database ? I was thinking in puting the health_plans (a,b) as columns, and the doctors as rows, with 0s and 1s to identify their covered health_plans.
Someone told me this was a misuse of a ManyToManyField, I don't know wich step to take.
Help appreciated
The approach of puting the health_plans as columns is not necessarily wrong, but it implies that you have a fixed number of health plans and that you will never add a new one.
The traditional approach for many-to-many relationships in relational databases is to introduce a table in the middle. This table will just contain the association between a doctor and a health plan.
If you have a Doctor table that contains:
id name
1 foo
2 bar
And a HealthPlan table:
id model
1 a
2 b
You then add a table Doctor_HealthPlan that is like:
doctor_id healthplan_id
1 2
2 1
2 2
The ManyToMany field type in django will automatically create this table for you. Your code is correct, but you should probably rename hp_id to something like health_plans, since it is a proxy that allows you to access the list of health plans associated to a doctor.
Django's ORM already takes care of the intermediate table so you don't have to "make this relation(ship) in the database", but given your question you obviously need to learn about proper relational model normalisation - if you don't understand the relational model you won't get nowhere with Django's ORM, nor with any other sql stuff FWIW.
For the record, in the relational model, a many to many relationship is modeled as a relation ("table" in SQL) with foreign keys on both other tables, ie:
health_plan(#health_plan_id, name, ...)
doctor(#doctor_id, firstname, lastname, ...)
doctors_health_plans(#health_plan_id, #doctor_id)
So your django models should be:
class HealthPlan(models.Model):
# no need to define an 'id' field,
# the ORM provides one by default
name = models.CharField(....)
class Doctor(models.Model):
firstname = models.CharField(....)
lastname = models.CharField(....)
health_plans = models.ManyToManyField(HealthPlan, related_name="doctors")
Then you'll be able to get all HealthPlans for a Doctor :
doc = Doctor.objects.get(pk=xxxx)
doc.health_plans.all()
and all Doctors for an HealthPlan:
plan = HealthPlan.objects.get(pk=xxxx)
plan.doctors.all()
The FineManual(tm) is your friend as usual...
You just need to save the two models first then add the healthplan instance to the doctors list. Django will handle the rest for you .
For example :
doctor_list = Doctors_list(name="Bwire")
health_plan.save()
doctor_list.save()
#Then add the plan to the doctors list.
doctor_list.hp_id.add(health_plan)
Django creates the tabels for you. In your project folder run:
python manage.py syncdb
Health_plan and Doctors_list are both tables.
'a' and 'b' are columns in Health_plan. 'Name' and 'hp_id' are columns in Doctors_list.
Django will create a column for id in each table. Django will also create a table "Doctor_list_Health_plan" to store the relation information.
Django models are Python classes, so the Python naming conventions apply. Use HealthPlan and Doctor (CapitalizeWord singular).
Your field names are a bit abstract. I suggest you use more descriptive names. Eg:
class HealthPlan(models.Model):
name = models.CharField()
extra_care = models.BooleanField()