How to do right relationships between objects Django - python

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)

Related

how to store multiple PRODUCTS's 'primary key' field in one ORDER's 'Order_Product' field?

in a nutshell, i want to select multiple PRODUCT per one ORDER entry. should i use some string manipulation method to compose all selected product in one string and save it into single field ? it sounds obvious but i don't think its way to go because its like majority of database design confronts the order and its multiple products and there should be a better way to deal with it. please suggest a better practice or something magical like one can store multiple primary key in one foreign key field but seems like thats not the best practice somehow. so what should i do ?
Order_Product = models.ForeignKey(Product, on_delete=models.CASCADE)
# im thinking of making below field into a string holding container
# and join many primary key and just outright save it separating by commas
Order_SelectedProduct = models.CharField(max_length=1000)
Order_Amount = models.CharField(max_length=10)
Order_Qty = models.CharField(max_length=10)
Order_Date = models.CharField(max_length=10)
Order_Deadline = models.CharField(max_length=10)
Order_Reference = models.CharField(max_length=128)
def __str__(self):
return self.Order_Id
obviously that Order_SelectedProduct field is not ideal way because one primary takes 14 spaces so say one order can only be limited to store only 71 products per order, thats more than enough but thats not scalable. i hope it yields something fruitfull
What you need is many-to-many relationship.
For this you can define your models as:
models.py
class Product(models.Model):
....
def __str__(self):
return self.id
class Order(models.Model):
Order_Product = models.ManyToManyField(Product)
...
Then, to add multiple products in the order object, you can do something like:
order = Order() --> create a order object
order.Order_Product.add(product1)
order.Order_Product.add(product2)
# product1 and product2 are the product objects that were selected
# and so on
order.save()
For more clarification, you can refer to django many-to-many relationship

could not understand foreign key and manytomany field in django

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.)

Django - QuerySet - Get count for all determined child objects inside all parent objects

I've got this model:
class Order(models.Model):
#bunch of fields#
class Laptop(models.Model):
#bunch of fields#
class Camera(models.Model):
#bunch of fields#
class MusicPlayer(models.Model):
#bunch of fields#
The last three have a foreign key associated to the Order class. I need to retrieve via a QuerySet the summed count for each child object for all orders, grouped by each one of them.
The result should look something like this:
Laptop:5
Camera:10
MusicPlayer:1
I've tried using different django queries but all I get to is retrieving the count for each order instead the count for all orders.
I know this could be easily done by just querying each one separately but I was requested to do all in one query.
Add related_name to your models:
class Laptop(models.Model):
order = models.ForeignKey(Order, related_name='laptops')
class Camera(models.Model):
order = models.ForeignKey(Order, related_name='cameras')
class MusicPlayer(models.Model):
order = models.ForeignKey(Order, related_name='music_players')
And then you can retrieve number of related models using annotate and Count:
from django.db.models import Count
orders = Order.objects.annotate(laptops_count=Count('laptops'), cameras_count=Count('cameras'), music_players_count=Count('music_players'))
print(orders.laptops_count)
print(orders.cameras_count)
print(orders.music_players_count)

Django ForeignKey _set on an inherited model

I have two models Category and Entry. There is another model ExtEntry that inherits from Entry
class Category(models.Model):
title = models.CharField('title', max_length=255)
description = models.TextField('description', blank=True)
...
class Entry(models.Model):
title = models.CharField('title', max_length=255)
categories = models.ManyToManyField(Category)
...
class ExtEntry(Entry):
groups= models.CharField('title', max_length=255)
value= models.CharField('title', max_length=255)
...
I am able to use the Category.entry_set but I want to be able to do Category.blogentry_set but it is not available. If this is not available,then I need another method to get all ExtEntryrelated to one particular Category
EDIT
My end goal is to have a QuerySet of ExtEntry objects
Thanks
I need another method to get all ExtEntryrelated to one particular Category
Easy:
ExtEntry.objects.filter(categories=my_category)
Do you know if there is a way to use the _set feature of an inherited
I don't know if there is a direct they for that. It is not mentioned in documentation.
But it is possible to get similar results with the select_related.
for e in category.entry_set.select_related('extentry'):
e.extentry # already loaded because of `select_related`,
# however might be None if there is no Extentry for current e
It is possible to select only entries which has ExtEntry:
for e in category.entry_set.select_related('extentry').exlude(extentry=None):
e.extentry # now this definitely is something, not None
Bad thing about the exclude is that it generates terrybly inefficient query:
SELECT entry.*, extentry.* FROM entry
LEFT OUTER JOIN `extentry` ON (entry.id = extentry.entry_ptr_id)
WHERE NOT (entry.id IN (SELECT U0.id FROM entry U0 LEFT OUTER JOIN
extentry U1 ON (U0.id = U1.entry_ptr_id)
WHERE U1.entry_ptr_id IS NULL))
So my resume would be: use ExtEntry.objects.filter() to get your results. The backwards relations (object.something_set) is just a convenience and does not work in every situation.
See the documentation here for an explanation of how this works.
Basically, since you can get the parent model item, you should be able to get its child because an implicit one-to-one linkage is created.
The inheritance relationship introduces links between the child model and each of its parents (via an automatically-created OneToOneField).
So, you should be able to do:
categories = Category.objects.all()
for c in categories:
entries = c.entry_set.all()
for e in entries:
extentry = e.extentry
print extentry.value
It isn't documented that I can see, but I believe that generally, your one-to-one field name will be a lower class version of the inheriting model name.
The problem your running into is because Entry and ExtEntry are in separate tables. This may be the best solution for you, but you should be aware of that when you choose to use multi-table inheritance.
Something like category.entry_set.exclude(extentry=None) should work for you.

How do I store multiple values in a single attribute

I don't know if I'm thinking of this the right way, and perhaps somebody will set me straight.
Let's say I have a models.py that contains this:
class Order(models.Model):
customer = models.foreignKey(Customer)
total = models.charField(max_length=10)
has_shipped = models.booleanField()
class Product(models.Model):
sku = models.charField(max_length=30)
price = models.charField(max_length=10)
Now, obviously an order would contain products and not just a product. What would be the best way to add products to an order? The only way I can think is to add another field to 'Order' called 'products', and fill it with a CSV with a sku for each product in it. This, for obvious reasons is not ideal, but I'm not very good at this stuff yet and have no idea of what the better way is.
(keep in mind this is pseudo code, so don't mind misspellings, etc.)
What you're after is a many to many relationship between product and order.
Something like:
class Order(models.Model):
customer = models.foreignKey(Customer)
total = models.charField(max_length=10)
has_shipped = models.booleanField()
products = models.ManyToManyField(Product)
see the docs here and here.
You can create one more model which will serve as many-to-many relationship between Order and Products
something like this
class OrderProducts(models.Model)
product = models.ForeignKey(Product)
order = models.ForeignKey(Order)
I don't recommend using ManyToMany field because prices needs to be saved as char field for future reference.
Make two models:
Order
With: Order number, customer details, date created, payment details
2.OrderItem
With : Product, Price, Quantity, Discount, SKU
And use OrderItem as inline in Order model

Categories