I have models like this:
class Vendor(models.Model):
title = models.CharField()
class Product(models.Model):
...
vendor = models.ForeignKey(Vendor, null=True, blank=True)
stock = models.ManyToManyField(Supplier, through='Stock')
class Stock(models.Model):
in_stock = models.BooleanField(default=True)
supplier = models.ForeignKey('catalog.Supplier', related_name='supplier_stock')
product = models.ForeignKey('catalog.Product', related_name='product_stock')
priority = models.IntegerField(default=0)
I designed models like this, because one Product can be supplied by different suppliers, and I need to know, what supplier exactly has this Product in stock.
So, in my view I want to get all results in values, to reduce number of queries and some specific logic. Also it duplicates me Product row with different Stock, by in python I group them up.
In my view I use:
Product.objects.all().values(
'id', 'title', 'vendor_code', 'vendor__title', 'price',
'product_stock__in_stock', 'stock__title', 'stock__id', 'stock__priority')
Because of INNER JOIN and null=True for Vendor related model, it returns me not all records for Product model. It just returns values where Vendor reference is set.
If I use 'vendor' instead of 'vendor__title' it returns me more results, than previous one, because in vendor field I can get {...'vendor': *id goes here*...} or {...'vendor': None...}, but I need the vendor__title value there. So any suggestions, how to achieve this?
Thanks in advance
Changed from vendor__title to product_stock__product__vendor__title helped me to fix my problem.
Related
I have general question. Let’s say I have a model field that is the sum of two other fields in a different model.
I’m having a hard time to implement this.
Let’s take the following example
model1
field1
field2
model2
field3 (dependent on field1 and field2) in model1
If I do it as part of specific page in my webapp. It means that if field1 or field2 has changed but the person didn’t visit the page that sum up the value and update it in field3 then field3 will carry incorrect value.
The only way to takle such a problem that I managed to identify is to never create field3. everytime a sum(or any other operation that had dependency on other fields) take a place is to be done in a variable inside the view.py
This means that value to be calculated everytime it is needed.
This way I won’t get myself in a position where I forget to recalculate the value of field3.
My question is this the best way to do it? Is there a way that whenever a depedent field change such as field1 that automatically change field3 without the need to visit a specific page?
I tried something with foriegn keys for field3 and try to add the value of two foriegn key inside the model.py but I don’t think it is allowed.
field3 = field1+ field2
any suggestions?
**
added the following example per request to further clarify question
If you notice that the totalPrice under transaction table is based on the price for the item and shipping. However, this require visiting order.html page.
My question if someone changed the item that resulted in a different price. Then without visiting the order.html page the totalprice in transaction table won't reflect the new price. Is there a way to build the "transaction" model in a way that updates the totalprice if any other field it depends on was updated without the need to visit the order.html page?
**
models.py
class Item(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
price = models.FloatField(null=True)
class Shipping(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
price = models.FloatField(null=True)
class Transaction(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
total_price = models.FloatField(null=True)
views.py
def order(request):
item_obj = item.object.get(user=self.user)
ship_obj = shipping.object.get(user=self.user)
trans_obj = transaction.object.get(user=self.user)
trans_obj.total_price = item_obj.price + ship_obj.price
trans_obj.save()
return render(request, 'order.html')
You can override save function in Shipping and Item models.
def save(self, *args, **kwargs):
#change total_price
# Transaction.save(update_fields['total_price'])
super(Item, self).save(*args, **kwargs)
However "#change total_price" will depend on how you are going to relate your models and how you're going to find proper Shipping object
My question is about creating a query which filter objects that are related through several intermediate tables. My relational database looks like this:
Any number of products can be uploaded by one user (one to many relationship). However, users also can rank products. A ranking can be completed by several users and a user can have several rankings (Many to many relationship). The same applies between Product and Ranking. I use explicit intermediate tables (Rank and Belong) which defines the M2M relationships by the through parameter, because they have additional information which describes the relationship.
The models code is something like this (I omitted irrelevant fields for simplicity):
class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
belong= models.ManyToManyField(Ranking, through="Belong")
#...
#The M2M table which relates Product and Ranking
class Belong(models.Model):
ranking = models.ForeignKey(Ranking, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
#...
class Meta:
unique_together = (("ranking", "product"))
class Ranking(models.Model):
user= models.ManyToManyField(settings.AUTH_USER_MODEL, through="Rank")
created = models.DateTimeField(auto_now_add=True)
#...
#The M2M table which relates User and Ranking
class Rank(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
ranking = models.ForeignKey(Ranking, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
#...
class Meta:
unique_together = (("user", "ranking"))
#The AUTH_USER_MODEL, which is not included here
My question is: How can I create a query which filters products which has been ranked by a given user? This implies “following back” relations between Belong, Ranking and Rank tables. I tried this following the Django tutorial, but it didn´t work:
Product.objects.filter(belong__ranking__rank__user=”username”)
You're a bit confused between your M2M relationships, and their through models.
For example, I don't understand why your M2M from Product to Ranking is called "belong". It should be called "rankings". Your M2M from Ranking to User at least has the right basic name, but it points to many users so should be "users".
Nevertheless, the point is that when you follow the M2Ms, you don't need to take the through tables into account. And the other issue is that "user" is itself a model, so to compare with a username you would need to continue to follow to that field. So:
Product.objects.filter(belong__user__username="username")
I have a situation where I need to do something similar to rendering a formset within a formset. But I'd rather focus on the problem before jumping to a solution.
In English first:
I'm creating a shipment from a warehouse.
Each shipment can contain multiple lines (unique combinations of product_type and package_type) with an item_count
However for each line there could be multiple "Packages" - a package_type of a product_type that has an item_count. Think of this as a batch.
The customer is only interested in seeing one line for each product_type/package_type
But we need to pull out the stock and correctly attribute the particular units from each batch to allow stock control, recall control etc to function. Therefore the dispatch staff IS interested in exactly which Packages are shipped.
Add to this the sales staff enter a SalesOrder that only specifies the product_type/package_type. They aren't interested in the Packages either. (Think putting in a forward order for next month - who knows what will be in stock then?).
Now the models (simplified for clarity):
class Package(models.Model):
create_date = models.DateField()
quantity = models.FloatField()
package_type = models.ForeignKey(PackageType, on_delete=models.PROTECT)
product_type = models.ForeignKey(ProductType, on_delete=models.PROTECT)
class CheckOut(models.Model):
package = models.ForeignKey(Package, on_delete=models.PROTECT)
create_date = models.DateField()
quantity = models.FloatField()
class Shipment(models.Model):
sales_order = models.ForeignKey(SalesOrder, null=True, blank=True)
ship_date = models.DateField(default=date.today,
verbose_name='Ship Date')
class ShipmentLine(models.Model):
shipment = models.ForeignKey(Shipment, null=True, blank=True)
sales_order_line = models.ForeignKey(SalesOrderLine, null=True, blank=True)
quantity = models.FloatField(verbose_name='Quantity Shipped')
checkout = models.ManytoManyField(CheckOut)
I currently have it working well with the constraint of a 1:M relationship of CheckOut:ShipmentLine. However when changing this to a M:M, things get knarly form-wise.
In the 1:M version the Shipment form (plus formset for the ShipmentLines) looks like this:
class CreateShipmentForm(forms.ModelForm):
class Meta:
model = om.Shipment
contact = forms.ModelChoiceField(
queryset=om.Contact.objects.filter(is_customer=True, active=True),
label='Customer')
customer_ref = forms.CharField(required=False, label='Customer Reference')
sales_order = forms.ModelChoiceField(queryset=om.SalesOrder.objects.all(),
required=False, widget=forms.HiddenInput())
number = forms.CharField(label='Shipment Number', required=False,
widget=forms.TextInput(attrs={'readonly': 'readonly'}))
class CreateShipmentLineForm(forms.ModelForm):
class Meta:
model = om.ShipmentLine
widgets = {
'checkout': forms.HiddenInput()
}
fields = ('package', 'quantity', 'id',
'sales_order_line', 'checkout')
id = forms.IntegerField(widget=forms.HiddenInput())
sales_order_line = forms.ModelChoiceField(
widget=forms.HiddenInput(), required=False,
queryset=om.SalesOrderLine.objects.all())
package = forms.ModelChoiceField(required=True, queryset=None) # queryset populated in __init__, removed for brevity
So for the 1:M, I could select a package, set the quantity and done.
For M:M, I will need to select product_type, package_type, and then 1 or more packages, AND for each package a quantity. (I'll be using JS in the form to filter these)
In my mind's eye I have a few possibilities:
create a (child) formset for the Packages and quantities and include in each line of the (parent) formset
create some sort of multi-field, multi-value matrix custom form field and use that
construct a modal dialog where the M:M stuff happens and somehow save the result to the form where validation, saving happens.
I hope I have explained it correctly and clearly enough. It's the most complex application of Django forms I've encountered and I'm not sure what the limitations/pros/cons of each of my options is.
Has anyone encountered this situation and have a solution? Or any words to the wise?
My thanks in advance,
Nathan
I have a similar situation, I am doing something like your second and third options:
I have overridden __init__() and, after calling super, I have a loop that adds a value selector for every field (of course you could use a single custom element here)
Then override save() and after calling super I process the extra field adding all the values.
In my primary class model Deals, I have certain fields as description, price, date_created etc. I now have to add some fields having sub-fields to it. For eg, I'm trying to add an age field to Deals. This age field further has subfields (like score_for_kid, score_for_baby, score_for_old etc), and I want to edit these scores from the admin.
Here is my models.py:
class Deals(models.Model):
description = models.TextField()
price = models.DecimalField(max_digits=7, decimal_places=2)
url = models.URLField(verify_exists=False)
currency = models.CharField(max_length=3)
created_date = models.DateField(auto_now_add=True)
kid_score = models.IntegerField(max_length=2,default=0)
teenager_score = models.IntegerField(max_length=2,default=0)
youth_score = models.IntegerField(max_length=2,default=0)
old_score = models.IntegerField(max_length=2,default=0)
I don't want to store all these sub fields (around 20-25 in 4 different fields) in the model, instead an age field connected to these subfields. Would a ManyToManyField work for this?
The underlying requirement is that when a user selects a subfield (say kids) on the browser, all the objects having higher kid scores are displayed.
I'm very new to Django and any help on this would be great. Thanks.
If I understand your question properly ou need to use ForeignKey fields.
class Deals(models.Model):
description = models.TextField()
price = models.DecimalField(max_digits=7, decimal_places=2)
#...
age = models.ForeignKey(Age)
class Age(models.Model):
kid_score = models.IntegerField(max_length=2,default=0)
teenager_score = models.IntegerField(max_length=2,default=0)
#...
Have a good read of the docs on Models. You might also find it useful to do some reading on relational databases / basic sql.
When you come to edit your objects in the django admin, you'll probably want to use an InlineModelAdmin class.
UPDATE
re-reading your question, it sounds like you might simply want to show / hide these additional fields on the main Deal model. If this is the case then you want to use fieldsets in the admin, with a class 'collapse'. There's an example in the docs.
If you want each Deal record to have multiple kid_score's associated with it then you want a foreign key. If each Deal can only have one kid_score then you need to keep the kid_score (and other) fields in the main model (if this is confusing then definitely do some reading on sql / relational databases).
To describe the system quickly, I have a list of Orders. Each Order can have 1 to n Items associated with it. Each Item has a list of ItemSizes. Given the following models, which have been abbreviated in terms of fields for this question, my goal is to get a distinct list of ItemSize objects for a given Order object.
class ItemSize(models.Model):
name = models.CharField(max_length=10, choices=SIZE_CHOICES)
class Item(models.Model):
name = models.CharField(max_length=100)
sizes = models.ManyToManyField(ItemSize)
class OrderItem(models.Model):
order = models.ForeignKey(Order)
item = models.ForeignKey(Item)
class Order(models.Model):
some_field = models.CharField(max_length=100, unique=True)
So... if I have:
o = Order.objects.get(id=1)
#how do I use the ORM to do this complex query?
#i need o.orderitem_set.items.sizes (pseudo-code)
In your current set up, the answer by #radious is correct. However, OrderItems really shouldn't exist. Orders should have a direct M2M relationship with Items. An intermediary table will be created much like OrderItems to achieve the relationship, but with an M2M you get much simpler and more logical relations
class Order(models.Model):
some_field = models.CharField(max_length=100, unique=True)
items = models.ManyToManyField(Items, related_name='orders')
You can then do: Order.items.all() and Item.orders.all(). The query you need for this issue would be simplified to:
ItemSize.objects.filter(item__orders=some_order)
If you need additional data on the Order-Item relationship, you can keep OrderItem, but use it as a through table like:
class Order(models.Model):
some_field = models.CharField(max_length=100, unique=True)
items = models.ManyToManyField(Items, related_name='orders', through=OrderItem)
And you still get your simpler relationships.
ItemSize.objects.filter(items__orderitems__order=some_order)
Assuming you have reverse keys like:
ItemSize.items - reverse fk for all items with such size
Item.orderitems - reverse for all orderitems connected to item
Item.orders - you can guess ;)
(AFAIR that names would be choose by default, but I'm not sure, you have to test it)
More informations about reverse key queries are available in documentation.