Django - UNIQUE constraint failed - python

I could'nt solve my problem even if I looked other people who had this problem.
How to create an id with foreign key? And how can I use the id?
This is my code:
class Receipt(models.Model):
amount = models.DecimalField(max_digits=5, decimal_places=2, primary_key=True)
vat = models.DecimalField(max_digits=5, decimal_places=2)
total_amount = models.IntegerField(default=0)
class ReceiptItem(models.Model):
receipt_id = models.ForeignKey('Receipt', on_delete=models.CASCADE)
product_id = models.ForeignKey('Product', on_delete=models.CASCADE)
amount = models.DecimalField(max_digits=5, decimal_places=2)
vat = models.DecimalField(max_digits=5, decimal_places=2)
vat_rate = models.DecimalField(max_digits=5, decimal_places=2)
sub_total = models.IntegerField(default=0)
name = models.CharField(max_length=200)
class Product(models.Model):
name = models.CharField(max_length=5)
vat_rate = models.DecimalField(max_digits=5, decimal_places=2)
amount = models.IntegerField(default=0, primary_key=True)
This is the error I'm getting:
django.db.utils.IntegrityError: UNIQUE constraint failed: migros_product.amount

class Product(models.Model):
name = models.CharField(max_length=5)
vat_rate = models.DecimalField(max_digits=5, decimal_places=2)
amount = models.IntegerField(default=0, primary_key=True)
Amount is set as primary key, which means that the database will only allow 1 record per value of amount.
BUT, you've also set a default value of 0, so your DB can only create 1 record with amount = 0, whenever the amount value is not provided.
You must decide what you want. If the amount uniqueness matters, then remove default=0 and put null=True, blank=True, for example.

Related

Query Django ORM three tables with totals

I have three tables CATEGORIES, TIPODESPESA and EXPENSE. I would like to perform a simpler and faster query to return the total expenses grouped by TYPODESPESA and by CATEGORY.
The expected result is the one below:
enter image description here
What I would like is a result in which the totals are grouped first by CATEGORY, then by TYPE EXPENSE and then show the EXPENSES.
However, for me to be able to do this, I perform the following queries below and I'm trying to simplify and make it faster, but I'm still learning a few things. If you can help with the query.
registros = CategoriaDespesa.objects.filter(
tipodespesas__tipodespesa_movimentodespesa__data__lte=data_final,
tipodespesas__tipodespesa_movimentodespesa__data__gte=data_inicio, ) \
.order_by('description')
categorias_valores = CategoriaDespesa.objects.filter(
tipodespesas__tipodespesa_movimentodespesa__data__lte=data_final,
tipodespesas__tipodespesa_movimentodespesa__data__gte=data_inicio,
tipodespesas__tipodespesa_movimentodespesa__company=company.company) \
.values('id').annotate(total=Coalesce(Sum(
F("tipodespesas__tipodespesa_movimentodespesa__valor"), output_field=FloatField()), 0.00))
tipos_valores = TipoDespesa.objects.filter(
tipodespesa_movimentodespesa__data__lte=data_final,
tipodespesa_movimentodespesa__data__gte=data_inicio,
tipodespesa_movimentodespesa__company=company.company).values('id') \
.annotate(total=Coalesce(Sum(F("tipodespesa_movimentodespesa__valor"), output_field=FloatField()), 0.00))
The classes I'm using are:
class CategoriaDespesa(models.Model):
description = models.CharField(max_length=200, help_text='Insira a descrição')
active = models.BooleanField(default=True, help_text='Esta ativo?', choices=CHOICES)
user_created = models.ForeignKey(User, on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class TipoDespesa(models.Model):
category = models.ForeignKey(
CategoriaDespesa, on_delete=models.CASCADE, related_name="tipodespesas")
description = models.CharField(max_length=200, help_text='Insira a descrição')
active = models.BooleanField(default=True, help_text='Esta ativo?', choices=CHOICES)
user_created = models.ForeignKey(User, on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Despesa(models.Model):
company = models.ForeignKey(
Company, on_delete=models.CASCADE, related_name="companies_movimentodespesa")
tipodespesa = models.ForeignKey(
TipoDespesa, on_delete=models.CASCADE, related_name="tipodespesa_movimentodespesa")
description = models.CharField(
max_length=200, help_text='Insira a descrição')
data = models.DateField(help_text="Data do Movimento",
default=datetime.now, blank=False)
valor = models.DecimalField(
max_digits=8, decimal_places=2, default=0.00, help_text='Valor')
user_created = models.ForeignKey(User, on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
My goal is to create a report that shows the levels and summed values ​​by level.

Referencing foreign key in Django Python

How to assign a product in ProductInStore to an instance of a Product already in store? Basically i have a scrapper and im looping through all the products and i need to first create the Product instance, and then A ProductInStore instance which is connected to Product via foreignKey. And when i try to put in ProductInStore(product=id) thats how i wanted to reference that Product, i get an error ValueError: Cannot assign "11393": "ProductInStore.product" must be a "Product" instance.
Do you have any idea how to reference it?
class Product(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True, null=False, editable=False)
created_at = models.DateTimeField(editable=False, default=timezone.now)
updated_at = models.DateTimeField(default=timezone.now)
product_category = models.ManyToManyField(EcommerceProductCategory)
description = RichTextField(max_length=2000, null=True, blank=True)
product_producer = models.ForeignKey('ProductProducer', on_delete=models.CASCADE)
creator = models.ForeignKey('users.CustomUser', on_delete=models.CASCADE, null=True, blank=True, related_name='product_creator')
points_from_reviews = models.DecimalField(max_digits=6, decimal_places=2, default=0, help_text='Średnia ocena produktu')
unique_id = models.CharField(max_length=256, unique=True)
type_of_unique_id = models.CharField(max_length=64)
class ProductInStore(models.Model):
product = models.ForeignKey('Product', on_delete=models.CASCADE)
store = models.ForeignKey('Store', on_delete=models.CASCADE)
price = models.DecimalField(max_digits=6, decimal_places=2, default=0, help_text='Cena w sklepie')
currency = models.CharField(max_length=4)
url = models.CharField(max_length=200)
created_at = models.DateTimeField(editable=False, default=timezone.now)
updated_at = models.DateTimeField(default=timezone.now)
# ADD TO DATABASE
try:
db_product = Product.objects.create(name=name, slug=slug, product_producer_id=3, unique_id=id,
description=description)
db_product.product_category.set(parsed_categories)
except:
print('product already in database')
ProductInStore.objects.create(product=,store=1,price=price,currency='PLN',url=url)
You can simply do this:
ProductInStore.objects.create(product_id=id, store=1, price=price, currency='PLN', url=url)
You don't need to pass the object if you have the ID of it, simply append _id to the field name and you can reference the foreign key that way.

How to assign the same value to multiple variables in a class in django models

In the below class orders, I want to assign the same value of weight to the amount
class Orders(models.Model):
consignment_id = models.IntegerField(primary_key='consignment_id', auto_created=True)
order_date = models.DateTimeField(default=timezone.now)
weight = models.FloatField()
from_place = models.CharField(max_length=20, null=True)
destination = models.CharField(max_length=20)
amount = models.FloatField(weight)
name = models.ForeignKey(User, on_delete=models.CASCADE)
status = models.CharField(max_length=20, default='Pending')
You should use the save function in the model
class Orders(models.Model):
consignment_id = models.IntegerField(primary_key='consignment_id', auto_created=True)
order_date = models.DateTimeField(default=timezone.now)
weight = models.FloatField()
from_place = models.CharField(max_length=20, null=True)
destination = models.CharField(max_length=20)
amount = models.FloatField()
name = models.ForeignKey(User, on_delete=models.CASCADE)
status = models.CharField(max_length=20, default='Pending')
def save(self):
self.amount = self.weight
super().save(self)

How can I split orders by attributes in my API

I have a method that creates orders from the user's cart. For the courier to take an order from different restaurants, the order is divided into several. But at the moment I'm splitting the order just by the dish in the cart. How to make an order split by restaurants? that is, if a user orders 5 dishes from two different restaurants, then 2 orders were formed.
views.py
#action(methods=['PUT'], detail=False, url_path='current_customer_cart/add_to_order')
def add_cart_to_order(self, *args, **kwargs):
cart = Cart.objects.get(owner=self.request.user.customer)
cart_meals = CartMeal.objects.filter(cart=cart)
data = self.request.data
for cart_meal in cart_meals:
order = Order.objects.create(customer=self.request.user.customer,
cart_meal=cart_meal,
first_name=data['first_name'],
last_name=data['last_name'],
phone=data['phone'],
address=data.get('address', self.request.user.customer.home_address),
restaurant_address=cart_meal.meal.restaurant.address,
)
order.save()
return response.Response({"detail": "Order is created", "added": True})
models.py
class Order(models.Model):
"""User's order"""
customer = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name='related_orders')
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
phone = models.CharField(max_length=20)
cart_meal = models.ForeignKey(CartMeal, on_delete=models.CASCADE, null=True, blank=True)
restaurant_address = models.CharField(max_length=1024, null=True)
address = models.CharField(max_length=1024)
status = models.CharField(max_length=100, choices=STATUS_CHOICES, default=STATUS_NEW)
created_at = models.DateTimeField(auto_now=True)
delivered_at = models.DateTimeField(null=True, blank=True)
courier = models.OneToOneField('Courier', on_delete=models.SET_NULL, null=True, blank=True)
class CartMeal(models.Model):
"""Cart Meal"""
user = models.ForeignKey('Customer', on_delete=models.CASCADE)
cart = models.ForeignKey('Cart', verbose_name='Cart', on_delete=models.CASCADE, related_name='related_meals')
meal = models.ForeignKey(Meal, verbose_name='Meal', on_delete=models.CASCADE)
qty = models.IntegerField(default=1)
final_price = models.DecimalField(max_digits=9, decimal_places=2)
class Meal(models.Model):
"""Meal"""
title = models.CharField(max_length=255)
description = models.TextField(default='The description will be later')
price = models.DecimalField(max_digits=9, decimal_places=2)
discount = models.IntegerField(default=0)
restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE, null=True)
slug = models.SlugField(unique=True)
class Restaurant(models.Model):
"""Restaurant"""
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True)
address = models.CharField(max_length=1024)
owner = models.ForeignKey('Restaurateur', on_delete=models.CASCADE, null=True)
meals = models.ManyToManyField('Meal', related_name='related_restaurant', blank=True)
How can I do this, please help me
You can group your meals with respect to resturants.
import itertools
from core.models import CartMeal, Order
for restaurant, cart_meals in itertools.groupby(CartMeal.objects.order_by('meal__restaurant'), lambda s: s.meal.restaurant):
order = Order.objects.create(
customer=self.request.user.customer,
first_name=data['first_name'],
last_name=data['last_name'],
phone=data['phone'],
address=data.get('address', self.request.user.customer.home_address),
restaurant_address=cart_meal.meal.restaurant.address,
)
order.cart_meal.set([cart_meal for cart_meal in cart_meals])
Ref: The answer is formulated by taking help from following answer.
https://stackoverflow.com/a/57897654/14005534

Django multiple foreign key to a same table

I need to log the transaction of the item movement in a warehouse. I've 3 tables as shown in the below image. However Django response error:
ERRORS:
chemstore.ItemTransaction: (models.E007) Field 'outbin' has column name 'bin_code_id' that is used by another field.
which is complaining of multiple uses of the same foreign key. Is my table design problem? or is it not allowed under Django? How can I achieve this under Django? thankyou
DB design
[Models]
class BinLocation(models.Model):
bin_code = models.CharField(max_length=10, unique=True)
desc = models.CharField(max_length=50)
def __str__(self):
return f"{self.bin_code}"
class Meta:
indexes = [models.Index(fields=['bin_code'])]
class ItemMaster(models.Model):
item_code = models.CharField(max_length=20, unique=True)
desc = models.CharField(max_length=50)
long_desc = models.CharField(max_length=150, blank=True)
helper_qty = models.DecimalField(max_digits=10, decimal_places=4)
unit = models.CharField(max_length=10, blank=False)
def __str__(self):
return f"{self.item_code}"
class Meta:
verbose_name = "Item"
verbose_name_plural = "Items"
indexes = [models.Index(fields=['item_code'])]
class ItemTransaction(models.Model):
trace_code = models.CharField(max_length=20, unique=False)
item_code = models.ForeignKey(
ItemMaster, related_name='trans', on_delete=models.CASCADE, null=False)
datetime = models.DateTimeField(auto_now=False, auto_now_add=False)
qty = models.DecimalField(max_digits=10, decimal_places=4)
unit = models.CharField(max_length=10, blank=False)
action = models.CharField(
max_length=1, choices=ACTION, blank=False, null=False)
in_bin = models.ForeignKey(
BinLocation, related_name='in_logs', db_column='bin_code_id', on_delete=models.CASCADE, null=False)
out_bin = models.ForeignKey(
BinLocation, related_name='out_logs', db_column='bin_code_id', on_delete=models.CASCADE, null=False)
remarks = models.TextField(blank=True)
def __str__(self):
return f"{self.trace_code} {self.datetime} {self.item_code} {dict(ACTION)[self.action]} {self.qty} {self.unit} {self.in_bin} {self.out_bin}"
you have same db_column in two fields so change it
in_bin = models.ForeignKey(
BinLocation, related_name='in_logs', db_column='bin_code_id', on_delete=models.CASCADE, null=False)
out_bin = models.ForeignKey(
BinLocation, related_name='out_logs', db_column='other_bin_code', on_delete=models.CASCADE, null=False) /*change db_column whatever you want but it should be unique*/
If are linked to the same model name, You should use different related_name for each foreign_key filed . here is the exemple :
address1 = models.ForeignKey(Address, verbose_name=_("Address1"),related_name="Address1", null=True, blank=True,on_delete=models.SET_NULL)
address2 = models.ForeignKey(Address, verbose_name=_("Address2"),related_name="Address2", null=True, blank=True,on_delete=models.SET_NULL)
thank you for everyone helped. According to Aleksei and Tabaane, it is my DB design issue (broken the RDBMS rule) rather than Django issue. I searched online and find something similar: ONE-TO-MANY DB design pattern
In my case, I should store in bin and out bin as separated transaction instead of both in and out in a single transaction. This is my solution. thankyou.
p.s. alternative solution: I keep in bin and out bin as single transaction, but I don't use foreign key for bins, query both in bin and out bin for the bin selection by client application.

Categories

Resources