Answer the question
In order to leave comments, you need to log in
Is it possible to link two models "duplex"?
Hello, I'm making an intranet system for friends. There was a problem when creating groups and when linking users to groups. Let's say we have users (Students). And there are groups. Through the admin panel, you need to add users (models.User) -> created group. (models.SomeGroup).
Inside the group, I created the students ManyToManyField field with a binding to the User model.
Everything seems to be working, but I would like the User panel to display exactly where the person is. How can this be implemented? Here are the codes:
This is the user profile model
class Profile(models.Model):
user = models.OneToOneField(User, related_name='profile')
birth=models.DateField(null=True)
degree = models.CharField(max_length = 2,choices=DEGREE_CHOICE,default='B')
#image=models.ImageField(verbose_name="Image",null=True)
phone=models.CharField(max_length = 12,null=True)
image=models.ImageField(null=True,blank=True,
upload_to=upload_location,
height_field="height_field",
width_field="width_field",)
height_field = models.IntegerField(default=0,null=True,blank=True)
width_field = models.IntegerField(default=0,null=True,blank=True)
course = models.CharField(max_length=2,choices=COURSE_CHOICE,default='FB')
start = models.DateField(null=True,blank=True)
ended = models.DateField(null=True,blank=True)
lessons = models.IntegerField(default=0)
left = models.IntegerField(default=0)
group = models.ForeignKey(IELTSGroup,blank=True,null=True)
class IELTSGroup(models.Model):
name = models.CharField(max_length=50,null=False,unique=True)
teacher = models.ForeignKey(User,limit_choices_to={'is_staff': True},related_name="teacher",null=False,default=0)
description = models.TextField(null=True,blank=True)
students = models.ManyToManyField(User,limit_choices_to={'is_staff': False}, related_name="students",blank=True)
created = models.DateTimeField(default=timezone.now)
start = models.DateField(null=True,blank=True)
Answer the question
In order to leave comments, you need to log in
Rewrite related_name to user_groups. Students - this is from the side of the group, and from the side of the user - user_groups.
If you do as below, then the groups will be shown on the list view page, that is, on the page where the list of all users is. In general, it is better to link groups to profiles
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
list_display = (..., 'groups') # тут перечисляете все поля, которые должны показываться + в конец добавляете groups
def groups(self, user):
return ', '.join(user.user_groups.values_list('name', flat=True))
groups.short_description = 'groups'
Profile admin
class UserProfileInline(admin.StackedInline):
model = Profile
#TabularInline
list_display = ('groups')
#
# # @admin.register(User)
class UserAdmin(DjangoUserAdmin):
inlines = (UserProfileInline,)
def groups(self, user):
return ', '.join(user.user_groups.values_list('name', flat=True))
groups.short_description = 'groups'
class ActivateAdmin(admin.ModelAdmin):
model = Activate
list_display = ('user','hash','assigned_date')
admin.site.register(Activate, ActivateAdmin)
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
class Profile(models.Model):
user = models.OneToOneField(User, related_name='profile')
birth=models.DateField(null=True)
degree = models.CharField(max_length = 2,choices=DEGREE_CHOICE,default='B')
#image=models.ImageField(verbose_name="Image",null=True)
phone=models.CharField(max_length = 12,null=True)
image=models.ImageField(null=True,blank=True,
upload_to=upload_location,
height_field="height_field",
width_field="width_field",)
height_field = models.IntegerField(default=0,null=True,blank=True)
width_field = models.IntegerField(default=0,null=True,blank=True)
course = models.CharField(max_length=2,choices=COURSE_CHOICE,default='FB')
start = models.DateField(null=True,blank=True)
ended = models.DateField(null=True,blank=True)
lessons = models.IntegerField(default=0)
left = models.IntegerField(default=0)
# groups = models.ForeignKey(IELTSGroup,blank=True,null=True)
groups = models.ManyToManyField(IELTSGroup, related_name="user_groups",blank=True)
class IELTSGroup(models.Model):
name = models.CharField(max_length=50,null=False,unique=True)
teacher = models.ForeignKey(User,limit_choices_to={'is_staff': True},related_name="teacher",null=False,default=0)
description = models.TextField(null=True,blank=True)
students = models.ManyToManyField(User,limit_choices_to={'is_staff': False}, related_name="user_groups",blank=True)
created = models.DateTimeField(default=timezone.now)
start = models.DateField(null=True,blank=True)
#dayOfTheWeek = DayOfTheWeekField(null=True,blank=True)
def __str__(self):
return self.name + " - " + self.teacher.username
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question