django-models

Filter queryset based on related object's field value

Filter queryset based on related object's field value Question: I have two models: class PartUse(models.Model): … imported = models.BooleanField() class PartReturn(models.Model): partuse = models.ForeignKey(PartUse) … imported = models.BooleanField() class PartUseListView(ListView): model = PartUse def get_queryset(self): if self.request.GET.get(‘show_imported’, None): qs = self.model.objects.all() else: qs = self.model.objects.filter(Exists(PartReturn.objects.filter( imported=False, id__in=OuterRef("partreturn")))) return qs I want to filter QuerySet for …

Total answers: 1

How to save fingerprint data in django

How to save fingerprint data in django Question: So I have a project where I have a model called Beneficiary where I need to store the fingerprint. This is the model: class Beneficiary(models.Model): image=models.ImageField(upload_to=’home/images/’,blank=True,null=True) name = models.CharField(max_length=200) gender=models.CharField(max_length=6,choices=GENDER_CHOICES,default=’male’) dob=models.DateField() registration_date=models.DateField() I want to save the fingerprints of the beneficiaries in the model. I am using …

Total answers: 1

django UniqueConstraint violation_error_message message not handled

django UniqueConstraint violation_error_message message not handled Question: I have this model: class DataMapping(models.Model): company = models.ForeignKey(Company, on_delete=models.CASCADE) location = models.ForeignKey(Location, null=True, blank=True, on_delete=models.CASCADE) department = models.ForeignKey(LocationDepartment, null=True, blank=True, on_delete=models.CASCADE) catagory = models.ForeignKey(CompanyCategory, on_delete=models.CASCADE) reason = models.ForeignKey(ReasonForProcessing, on_delete=models.CASCADE) class Meta: constraints = [ UniqueConstraint( name=’unique_data_map’, fields=[‘company’, ‘location’, ‘department’, ‘catagory’, ‘reason’], condition=Q(location__isnull=False), violation_error_message="This data map has already …

Total answers: 2

Unique together between field and model connected by ForeignKey django

Unique together between field and model connected by ForeignKey django Question: I have two models. class Post(models.Model): title = models.CharField(max_length=150) class Attachment(models.Model): type = models.CharField(choices=AttachmentChoices.get_choices(), max_length=15) link = models.URLField() post = models.ForeignKey(‘Post’, on_delete=models.PROTECT, related_name=’attachments’) I want to do something like this in meta class of Post model class Meta: unique_together = [(‘title’, ‘attachments’)] but it …

Total answers: 1

Iterate over Queryset backwards with Foreign Keys

Iterate over Queryset backwards with Foreign Keys Question: I am trying to iterate over a queryset for a certain model and want to go over a table to another table. My models are the following (shrinked to a smaller version): class Erp(models.Model): erp_id = models.AutoField(primary_key=True) target_item = models.ForeignKey(‘Items’, models.DO_NOTHING,related_name=’target_item’) class PE(models.Model): pe_id = models.AutoField(primary_key=True) item …

Total answers: 1

Try to Return dataset as a table with request of postman

Try to Return dataset as a table with request of postman Question: I wanna Request with postman and get a table of all books as return but I can’t find a way to do this. Thanks a lot This is my model of books: class Book(models.Model): name = models.CharField(max_length=100) username = models.ForeignKey(User, on_delete=models.CASCADE) publication_date = …

Total answers: 1

Display data by date, when data comes from multiple models in Django

Display data by date, when data comes from multiple models in Django Question: I’m currently working on an app with Django 4.1. I try to display a resume of some data comming from the database. Here is a simplified example of my models: class Signal(models.Model): data1 = models.CharField() data2 = models.CharField() date = models.DateTimeField() class …

Total answers: 2

Django ModelForm as a layer of data validation

Django ModelForm as a layer of data validation Question: I have an authorization form, and I get an error – "already have a user with this email". I want to use the forms as a layer of validating the data. I now, that i can pass instance of CustomUser in my form, but i can`t …

Total answers: 2

Sum together list of F expressions

Sum together list of F expressions Question: Is there a way to specify (in an annotation or aggregation) that a sequence of F expressions should be summed together without manually typing out F("first_prop") + F("second_prop") + …? I want something similar to how python’s sum() function allows you to pass an iterable and get the …

Total answers: 1