I have the following Django model:
class opetest(models.Model):
name = models.CharField(max_length=200)
people = models.ManyToManyField(User, blank=True)
This m2m relationship is available on User object as 'opetest_set'.
How can I make available to edit this m2m relationship in django admin 'User edit page'?
To edit m2m in Django admin use InlineModelAdmin. In order to use your own UserAdmin you should unregister User first, like this:
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
To make opetest accessible from User instance:
class opetest(models.Model):
name = models.CharField(max_length=200)
people = models.ManyToManyField(User, blank=True, related_name='opetests')
Then user.opetests.all()
Related
example field that i want to add in built in User model
class bulit_in_user_model(models.Model):
relationships = models.ManyToManyField('self',on_delete=models.CASCADE)
how to do that
Read more about referencing self using ManyToManyField.symmetrical here
from django.db import models
class MyUser(models.Model):
...
friends = models.ManyToManyField("self", blank=True)
Monkey patching could achieve your goal,but it's better to follow the normal User model Customization though
Inherit from AbstractUser, and specify AUTH_USER_MODEL = 'myapp.MyUser in your settings.
class MyUser(AbstractUser):
friends = models.ManyToManyField("self", blank=True)
I have extended my user model with the OneToOne model and would now like to be able to change additional information via the Wagtail user editing interface.
Customers App
#customer.model
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='customers')
org_title = models.CharField(max_length=500, default='', blank=True)
I have already seen this topic in wagtail docs: https://docs.wagtail.io/en/v2.9.3/advanced_topics/customisation/custom_user_models.html
But AbstractUser models are used. How can this be achieved with related models through the OneToOne relationship?
I have the following models
from django.contrib.auth.models import User
User = settings.AUTH_USER_MODEL
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
class Tutor(UserProfile):
# tutor_fields here
From User object how can I access Tutor? is it with user.profile?
user.profile.select_related('tutor') does not seem to work.
OneToOneField work like ForeignKey except unique=True and you don't need to specify the related_name (you can specify if you want to change it).
For you example:
from django.contrib.auth.models import User
User = settings.AUTH_USER_MODEL
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
class Tutor(UserProfile):
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, related_name='tutor')
details = models.CharField(max_length=100)
NB: Use a ForeignKey if you want/need one Tutor for many UserProfile, else if you want one (and only one) Tutor for one UserProfile
And you can access to the Tutor bu UserProfile by UserProfile.tutor.details.
As described in the django docs, you should be able to access it with user.user_profile.
Basically I am trying to recreate a fields in this model that is similar to django user authenticate system, for this I try using many-to-many fields but it ended up like this.
I am trying to have another field that can show what exist and another field for I have chosen similar to django admin, which looks like this.
This is my code
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
class BlogPage(models.Model):
category = models.ManyToManyField(Category)
title = models.CharField(max_length=128)
preview = models.TextField(max_length=256)
content = models.TextField()
I believe what you want is a filter_horizontal widget used in the Admin template. This should help: Django Admin ManyToManyField
currently i am able to display many to many fields is admin panel
models.py
class Abc(models.Model):
course = models.ManyToManyField(Course, blank=True)
admin.py
class AbcsAdmin(admin.ModelAdmin):
"""
Admin panel management for Alumni
"""
list_display = ["id","get_course"]
def get_course(self,obj):
return [course.name for course in obj.course.all()]
The site I'm building allows users to create "posts" and has a Twitter-like concept of followed users. For a given user, I'd like to show all of the posts from the users that they follow.
Here are my simplified models:
class User
# a standard django.contrib.auth user model
class UserProfile(models.Model):
# my AUTH_PROFILE_MODULE for django-profiles
user = models.ForeignKey(User, unique=True)
following = models.ManyToManyField('self', symmetrical=False, related_name="followed_by")
class Posts(models.Model):
user = models.ForeignKey(User)
post = models.TextField()
Question: How do you create a queryset of all Post objects from the Users that a given User is following?
I think I've made it more complicated by creating the "follow" relationship on UserProfile, which is not the model with the ForeignKey relationship with Posts.
UPDATE! Here's the answer:
Posts.objects.filter(user__userprofile__in=UserProfile.objects.get(user=your_user_object).following.all())
Posts.objects.filter(user__in=UserProfile.objects.get(user=your_user_object).following)