Django: Only one of the two fields can be filled

I have this model:

class Journals(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField("Code", max_length=50) name = models.CharField("Name", max_length=2000) publisher = models.CharField("Publisher", max_length=2000) price_euro = models.CharField("Euro", max_length=2000) price_dollars = models.CharField("Dollars", max_length=2000) 

Is there a way for people to fill in either price_euro or price_dollars ?

I know that the best way to solve the problem is to have only one field and another table that define the currency, but I have restrictions and I can not change the database.

Thanks!

+7
source share
4 answers

I agree with T. Stone that setting up a form is the best way. If you cannot do this, or in addition to this, you can apply the rule using the user-friendly clean () method in your form . Just keep in mind that any validation errors that you selected with the clean () method will not be bound to specific fields, but to the form itself.

+2
source

The interface does not have to reflect the data structure. You could simply specify your user interface with 1 currency field, and then choose between US dollars and euros, and then when the data is returned to the Journals model, you simply assign it to the correct field and clear the other.

+4
source

Since you can use the limitations of the Django 2.2 database. This means that in addition to the usual check at the model level (which has been working forever since then and was proposed by the user Tom), now you can make sure that invalid data cannot enter the database at all.

 # models.py from django.db import models from django.db.models import Q class Journals(models.Model): # [...] def clean(self): """Ensure that only one of 'price_euro' and 'price_dollars' can be set.""" if self.price_euro and self.price_dollars: raise ValidationError("Only one price field can be set.") class Meta: contraints = [ models.CheckConstraint( check=( Q(price_euro__isnull=False) & Q(price_dollars__isnull=True) ) | ( Q(price_euro__isnull=True) & Q(price_dollars__isnull=False) ), name='only_one_price', ) ] 

Be sure to create and apply the migration after changing Meta.constraints .

+2
source

I would do it using the clean () method of the model. Thus, it will work with the Admin site.

https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.clean

+1
source

Source: https://habr.com/ru/post/1308172/


All Articles