0

hi i'm trying to make a new foreignkey from different fields using to_field but it return product values instead no_invoice

class Model1(models.Model):
    admin = models.ForeignKey(User,on_delete=models.CASCADE)
    product = models.ForeignKey(Products,on_delete=models.CASCADE)
    no_invoice = models.IntegerField(unique=True)

    def __str__(self):
        return self.product.name

class Model2(models.Model):
    admin = models.ForeignKey(User,on_delete=models.CASCADE)
    invoice = models.ForeignKey(Model1,to_field='no_invoice',on_delete=models.CASCADE)

is there something else i have to add ? thanks

2 Answers 2

1

ForeignKey.to_field The field on the related object that the relation is to. By default, Django uses the primary key of the related object. If you reference a different field, that field must have unique=True.

In your case There is nothing to add.

When you try to fetch the foreign key field, It return all related Model1 objects in a QuerySet

your str method is wrong . you have to fetch it with self. like below

     def __str__(self):
                return self.product.name

By default this method returns id(primary key) of the model object As your str method is returning product name when you try to print or render the foreign key field the string representation of the Model1 object is returned i.e. Product name.

Instead if you want to get your no_invoice field

you can replace your str with


    def __str__(self):
              return self.no_invoice

or

you have access to the whole object. you can also do it this way in your views.py.

m2_object = Model2.objects.all().first()
print(m2_object.invoice.no_invoice) #this will give your no_invoice value

ref: https://docs.djangoproject.com/en/3.0/ref/models/fields/

2
  • yes , i forget to use self , its just a test code . i dont want to use it in str method
    – art_cs
    Commented Jul 25, 2020 at 9:37
  • when you fetch Model2.invoice field you will get Model1 related object, which has all Model 1 fields. There is no need of using str method Commented Jul 25, 2020 at 10:00
1

Since no_invoice is unique and seems like the serial number of Model1, can you just make that the primary key? Like this:

class Model1(models.Model):
    admin = models.ForeignKey(User,on_delete=models.CASCADE)
    product = models.ForeignKey(Products,on_delete=models.CASCADE)
    no_invoice = models.IntegerField(primary_key=True)

    def __str__(self):
        return self.product.name #self should be included

class Model2(models.Model):
    admin = models.ForeignKey(User,on_delete=models.CASCADE)
    invoice = models.ForeignKey(Model1,on_delete=models.CASCADE)

PS: You can also take a look at using a OneToOne relationship instead of Foreign Key if the relationship from Model2 to Model1 is OneToOne.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.