OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter Alin
  • Start date Start date
A

Alin

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

Here are my models:

Code:
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.

  • I tried doing this and it didn't work:

Code:
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'.
<p>So I surfed the web for this problems solutions but I couldn't quite find the answer to this problem.</p>
<p>Here are my models:</p>
<pre><code>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)
</code></pre>
<p>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.</p>
<ul>
<li>I tried doing this and it didn't work:</li>
</ul>
<pre><code>class WebsiteInline(admin.TabularInline):
model = Website
extra = 1


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



admin.site.unregister(User)
admin.site.register(User, UserAdmin)
</code></pre>
<p>and here is the error: <class 'accounts.admin.WebsiteInline'>: (admin.E202) 'accounts.Website' has no ForeignKey to 'auth.User'.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top