Alin
Alin

Reputation: 313

How to add a Tabular Inline to Django's default user's page When The other model has a many to many relation with user model

So I surfed the web for this problems solutions but I couldn't quite find the answer to this problem.

Here are my models:

class Website(models.Model):
    url = models.URLField()
    users = models.ManyToManyField(User)

    def __str__(self) -> str:
        return str(self.url)


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(null=True, blank=True)
    nickname = models.CharField(blank=True, null=True, max_length=50)
    location = models.CharField(blank=True, null=True, max_length=50)
    weight = models.DecimalField(null=True, max_digits=5, decimal_places=2)
    
    def __str__(self) -> str:
        return str(self.user)

So as you can see one field in the Website model has a "ManyToMany" relationship with django's default User model. So I was wondering How I could change or see the list of websites a user has in the User's page/section.

class WebsiteInline(admin.TabularInline):
    model = Website
    extra = 1


class UserAdmin(admin.ModelAdmin):
    inlines = [WebsiteInline]
    


admin.site.unregister(User)
admin.site.register(User, UserAdmin)

and here is the error: <class 'accounts.admin.WebsiteInline'>: (admin.E202) 'accounts.Website' has no ForeignKey to 'auth.User'.

Upvotes: 1

Views: 22

Answers (0)

Related Questions