0

I'm making a website where the Auth.User is extended by a Userprofile where it has a foreign key to the User itself aswell as a key to user who invited the user.

I am using related_name for both but it throws me an error:

ERRORS:
<class 'account.admin.UserProfileInline'>: (admin.E202) 'account.UserProfile' has more than one ForeignKey to 'auth.User'.

Heres model:

class UserProfile(models.Model):
    user = models.OneToOneField(User ,on_delete=models.CASCADE, related_name="user")
    invited_by = models.OneToOneField(User, related_name="invited_by")
    bought = models.ManyToManyField(product, blank=True)

    def __str__(self):
        return str(self.user)

Why does it still say that i have to many foreign keys? If i would change the name to a invalid related_name then it says that i need to add a correct one. I dont see my error...

Bolian
  • 41
  • 8
  • 1
    I believe you want to change from OneToOneField to a ForeignKey field. The name OneToOne suggests you can only have it doing just that, one to one. Which your model is setting up a Two to One, even if the related names are different. The difference between the two fields is explained in an answer [here](https://stackoverflow.com/questions/5870537/whats-the-difference-between-django-onetoonefield-and-foreignkey) – gallen Jul 28 '17 at 23:59
  • mm quite correct you are. I guess i'll have to live with the fact that the admin can change to make the user to be invited by more members than one. Thanks! – Bolian Jul 29 '17 at 00:02
  • Spoke too soon, it's still throwing same error even tho i changed both to foreign keys :/ – Bolian Jul 29 '17 at 00:05
  • I changed the invited_by to a manytomany and now it's working. Thanks for help! If you know any other way to make it so that it can only have 1 user as invited_by please do tell, but no real need cuz im looking it up myself. – Bolian Jul 29 '17 at 00:08
  • Hmmm. What version of Django are you using? – gallen Jul 29 '17 at 00:13
  • 1.11 , so the latest. – Bolian Jul 29 '17 at 00:20
  • 1
    Simply because I'm curious and want to know the answer to this, I did some digging. I'm beginning to theorize your issue is with the use of an InLine admin class, as mentioned from your posted error line. I found [this](https://stackoverflow.com/questions/44646643/admin-inline-with-no-foreignkey-relation) question dealing with issues related to inlines and FK relationships. It's not a direct solution to your issue, but it may spark some useful ideas. Cheers! – gallen Jul 29 '17 at 00:32
  • Thanks, i'm registring it as a admin.stackedinline in admin.py so that might be causing some wierdness. Havn't found a solution yet but i'll keep it be for now then return to it later. :) – Bolian Jul 29 '17 at 01:23

0 Answers0