foreign-keys

related_name parameter name conflict in django?

related_name parameter name conflict in django? Question: i have a query regarding "related_name" parameter (used while establishing a relationship between models) suppose i assign the value to related_name= "x" then, it sets the given value to reverse relation manager name ex- parent_model_object.x.all() it sets given name to parent model while filtering objects. ex – child_model.objects.filter(x__name …

Total answers: 1

Django showing error 'constraints' refers to the joined field

Django showing error 'constraints' refers to the joined field Question: I have two models Product and Cart. Product model has maximum_order_quantity. While updating quantity in cart, I’ll have to check whether quantity is greater than maximum_order_quantityat database level. For that am comparing quantity with maximum_order_quantity in Cart Model But it throws an error when I …

Total answers: 1

Django: How can I change my code to access ForeignKey fields through 3 tables?

Django: How can I change my code to access ForeignKey fields through 3 tables? Question: I tried to get values with using three tables. cart_c = Cart.objects.select_related(‘item’).filter(user=2) context = {‘cart_c’: cart_c} I got QuerySet, ‘cart_c’, but I could not refer ItemPhoto.photo from ‘cart_c’. So, I made three tables joined with prefetch_related(). items = Item.objects.prefetch_related(‘item_photo’, ‘cart_item’).filter(cart__user=2) …

Total answers: 1

SQLAlchemy cannot find referenced table via foreignkey in many-to-many relationship

SQLAlchemy cannot find referenced table via foreignkey in many-to-many relationship Question: I have already succeeded in adding one many-to-many relationship in my database. However, when trying to add another, I am met with: sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column ‘user_shiftTemplate.template_id’ could not find table ‘shifttemplate’ with which to generate a foreign key to target column …

Total answers: 2

django queryset filter on whether related field is empty

django queryset filter on whether related field is empty Question: here is my models: class Flag(models.Model): ban = models.ForeignKey(‘flags.Ban’, on_delete=models.CASCADE, related_name=’flags’) class Ban(models.Model): punished = models.BooleanField(default=None) Flag is triggered when user report some content. and they are summarised in to a Ban instance for administrator to verify. briefly, a ban can have many flags. there …

Total answers: 2

Why is model._meta.get_fields() returning unexpected relationship column names, and can this be prevented?

Why is model._meta.get_fields() returning unexpected relationship column names, and can this be prevented? Question: Imagine I have some models as below: class User(AbstractUser): pass class Medium(models.Model): researcher = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True, related_name="medium_researcher") old_medium_name = models.CharField(max_length=20, null=True, blank=True) class Uptake(models.Model): material_quality = models.CharField(max_length=20, null=True, blank=True) medium = models.ForeignKey(Medium, on_delete=models.CASCADE, blank=True, null=True, related_name="uptake_medium") Now I have …

Total answers: 1

Save FK on post in django

Save ForeignKey on post in django Question: I am having trouble with saving a fk in Infringer table on post. I am trying to save the customer ID when I add a record. For troubleshoot purposes I added a few print lines and this the out put. As you can see below the correct customer …

Total answers: 1

How can I do to access to a foreign key in the other side?

How can I do to access to a foreign key in the other side? Question: I am working a on projects using Django. Here is my models.py : class Owner(models.Model): name = models.CharField(max_length=200) class Cat(models.Model): owner = models.ForeignKey(Owner, on_delete=models.CASCADE) pseudo = models.CharField(max_length=200) I did that : first_owner = Owner.objects.get(id=1) And I would like to do …

Total answers: 2

Code to display Django (Reverse) related table throws error

Code to display Django (Reverse) related table throws error Question: I’m quite new to Django and practicing Models section of Django by following its official tutorial. I also created a project of my own and try to apply similar concepts. This is my models.py; from django.db import models class Experience(models. Model): o01_position = models.CharField(max_length=50) o02_year_in …

Total answers: 2

How to access ForeignKey properties from models.py in django

How to access ForeignKey properties from models.py in django Question: I want to access a property of another model from a model via ForeignKey. For example, if invoice_details is a ForeignKey to InvoiceMaster, then I want invoice_details to store the value of InvoiceMaster‘s invoice_number, instead of the primary key. How can I achieve this? I …

Total answers: 2