Answer the question
In order to leave comments, you need to log in
How to bind models in Django given conditions?
There are 2 models:
class Code(models.Model):
name = models.CharField(max_length=40, unique=True, verbose_name='Наименование')
used = models.BooleanField(default=False, verbose_name='Используется ')
class Ordering(models.Model):
num = models.PositiveIntegerField(auto_created=True, verbose_name='Номер заявки')
system = models.ForeignKey(Code, on_delete=models.DO_NOTHING)
Answer the question
In order to leave comments, you need to log in
Hey!
Something like this should work
link to the source
in admin.py
@admin.register(Ordering)
class OrderingAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "system":
kwargs["queryset"] = Code.objects.filter(used=True)
return super().formfield_for_foreignkey(db_field, request, **kwargs)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question