1👍
The solution would be:
A.objects.filter(barcode__contains='123456')
With that you would get a list of all objects which barcode contains the desired string.
Anyway, I would recommend to use a ForeignKey
relation, as it is a proper semantic solution:
https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
class A(models.Model):
name = models.Charfield(max_length=50)
class Bcode(models.Model):
barcode = models.IntegerField()
a = models.ForeignKey(A)
Then you can get all the Barcodes from an A
-instance:
a_instance.bcode_set.all()
and you can get the according A
-instance for a barcode for example:
b = Bcode.objects.get(123456)
b.a.name
Source:stackexchange.com