How to link models in Django given the conditions?

There are 2 models:

class Code(models.Model):
 name = models.CharField(max_length=40, unique=True, verbose_name='Name')
 used = models.BooleanField(default=False, verbose_name='Use ')

class Ordering(models.Model):
 num = models.PositiveIntegerField(auto_created=True, verbose_name='Number of order')
 system = models.ForeignKey(Code, on_delete=models.DO_NOTHING)

need to admin about adding the entries in the Ordering, in the field system show all fields from the model Code, only the field used where True. How to implement it?
April 19th 20 at 12:15
1 answer
April 19th 20 at 12:17
Something like this should work
source link
class OrderingAdmin(admin.ModelAdmin):

 def formfield_for_foreignkey(self, db_field, request, **kwargs):
 if == "system":
 kwargs["queryset"] = Code.objects.filter(used=True)
 return super().formfield_for_foreignkey(db_field, request, **kwargs)

Find more questions by tags Django