foreign-keys

How to sort a queryset based on a foreign key field?

How to sort a queryset based on a foreign key field? Question: This is a contest system project. I have these models and I know the contest_id and problem_id. I’m trying to return a queryset that contains users who have solved a problem. A user who solves a problem is the one whose submission’s score …

Total answers: 1

Django – i cant migrate my model "prestamo"

Django – i cant migrate my model "prestamo" Question: this is my model class Prestamo(models.Model): lector = models.ForeignKey( Lector, on_delete=models.CASCADE) libro = models.ForeignKey(Libro, on_delete=models.CASCADE , related_name=’libro_prestamo’) fecha_prestamo = models.DateField() fecha_devolucion = models.DateField(blank=True, null=True) devuelto = models.BooleanField() and this is my error in console return self.cursor.execute(sql, params) django.db.utils.IntegrityError: inserción o actualización en la tabla «lector_prestamo» viola …

Total answers: 1

Django Python Foreign Key issue

Django Python Foreign Key issue Question: I am showing "well" information linked to a specific project successfully based on well_id When I try to show drilling_tools in a similar fashion I get an error. Can anyone see what I am doing wrong in my views? def well_show(request, well_id): well = Well.objects.get(pk=well_id) drilling_tools = DrillingTool.objects.get(pk=well_id) return …

Total answers: 1

How do I access objects (user info) across Django models? (Foreign Key, Beginner)

How do I access objects (user info) across Django models? (Foreign Key, Beginner) Question: I have a Profile model and a Post model in my Django project: class Profile(models.Model): """ User profile data """ user = models.ForeignKey( User, on_delete=models.CASCADE, related_name=’profile’) first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) id_user = models.IntegerField() bio = models.TextField(max_length=280, blank=True) def __str__(self): …

Total answers: 1

DRF – Relate an object to another using views, serializers and foreign key

DRF – Relate an object to another using views, serializers and foreign key Question: Basically I have two models with one-to-one relationship. And I want retrieve information from one, when I call the other. My models: class Customer(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) class CurrentAccount(models.Model): customer_account = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name=’customer_account’, blank=True, null=True) balance = …

Total answers: 1

how to alter model field to foreign key with existing tables

how to alter model field to foreign key with existing tables Question: I have a simple model. This table has a few entries in the db. And, the category field of them is not empty: # blog.models.py from django.db import models class Article(models.Model): title = models.CharField(max_length=100) category = models.CharField(max_length=50) I want to change the category …

Total answers: 3

How can I change the db_column name of foreign key in Django?

How can I change the db_column name of foreign key in Django? Question: How can I change db_column of Foreign Key in Django? class A(models.Model): it_id = models.CharField(‘IT ID’, max_length=10) class B(models.Model): it_id = models.ForeignKey(‘A’, related_name=’item’, on_delete=models.PROTECT, db_column=’it_id’) when I call objects, >>> B.objects.all().values() <QuerySet [{‘it_id_id’}:’xxx’]> It is ‘it_id_id’…. How can I change ‘it_id’ from …

Total answers: 1

django.db.utils.IntegrityError: FOREIGN KEY constraint failed

django.db.utils.IntegrityError: FOREIGN KEY constraint failed Question: My models.py class Order(models.Model): user = models.ForeignKey(User, blank=True, null=True, on_delete=models.PROTECT) customer_email = models.EmailField(blank=True, null=True, default=None) customer_name = models.CharField(max_length = 64, blank=True, null=True, default=None) customer_phone = models.CharField(max_length = 48, blank=True, null=True, default=None) customer_address = models.CharField(max_length = 128, blank=True, null=True, default=None) total_price = models.DecimalField(max_digits=10, decimal_places=2, default=0) #total price for all products …

Total answers: 4

Add foreign key related entities in the same Django admin page

Add foreign key related entities in the same Django admin page Question: I’m working on a Django app which will manage some quizzes. Those quizzes are formed by a question and some possible answers, which I’ve defined as different models. There is an OneToMany relationship between them, which as far as I know, should be …

Total answers: 1