Ответы пользователя по тегу PostgreSQL
  • Возможно ли связать две модели "дуплексно"?

    dosya97
    @dosya97 Автор вопроса
    Fullstack web-developer
    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
    Ответ написан
    Комментировать