unique-constraint

How to code unique constraint per parent ForeignKey in Django model?

How to code unique constraint per parent ForeignKey in Django model? Question: Here’s my code: from django.db import models class Parent(models.Model): name = models.CharField(max_length=50, unique=True) def __str__(self): return str(self.name) class Child(models.Model): parent = models.ForeignKey(Parent, on_delete=models.CASCADE) name = models.CharField(max_length=50, unique=True) def __str__(self): return str(self.name) Parents in my database: Rogan Smith Doe In admin dashboard: First, I …

Total answers: 1

Django UniqueConstraint

Django UniqueConstraint Question: Context I have the models AppVersion, App & DeployApp. In the AppVersion model users can upload APK files to the filesystem. I am using a pre_save signal to prevent uploading APK files with the same version_code for a specific App like this: @receiver(pre_save, sender=AppVersion) def prevent_duplicate_version_code(sender, instance, **kwargs): qs = AppVersion.objects.filter(app_uuid=instance.app_uuid, version_code=instance.version_code) …

Total answers: 1

Django Unique Together (with foreign keys)

Django Unique Together (with foreign keys) Question: I have a situation where I want to use the Meta options of unique_together to enforce a certain rule, here’s the intermediary model: class UserProfileExtension(models.Model): extension = models.ForeignKey(Extension, unique=False) userprofile = models.ForeignKey(UserProfile, unique=False) user = models.ForeignKey(User, unique=False) class Meta: unique_together = ((“userprofile”, “extension”), (“user”, “extension”), # How can …

Total answers: 7