admin管理员组

文章数量:1332872

Django 4.2.5, python 3.11, saving to mysql db.

I am trying to update the "chat_user_status" field for my "Teacher" model, which extends the abstract User model.

Model.py code for Teacher:

class TeacherProfile(models.Model):
    teacher = models.OneToOneField(
        UserProfile, related_name='teacher', primary_key=True, parent_link=True, on_delete=models.CASCADE)
    chat_user_status = models.BooleanField(default=False, verbose_name='Chat Status')

    def save(self, *args, **kwargs):
            identity = self.teacher.email
            friendlyName = self.teacher.first_name + \
                ' ' + self.teacher.last_name
    
            super(TeacherProfile, self).save(*args, **kwargs)

Views.py (Teacher model) login code that is trying to update the chat_user_status field, and nothing is updating, and no error:

def login_user(request):
    logout(request)
    email = password = ''

    if request.POST:
        email = request.POST['email']
        password = request.POST['password']

        user = authenticate(username=email, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                if is_teacher(user):
                    user.chat_user_status=True
                    user.save()

Views.py relevant code from my chat app that is trying to save the data

def update_chatuser_status(uid):
    user_profile = UserProfile.objects.get(id=uid)
    teacher_profile = None

    try:
        teacher_profile = TeacherProfile.objects.get(teacher_id=user_profile.id)
    except:
        pass

    if teacher_profile != None:
        teacher_profile['chat_user_status']=False
        teacher_profile.save()

There is no change to the field 'chat_user_status' after calling save(). There is no error message. I have searched other answers, either they don't work, or are irrelevant.

Django 4.2.5, python 3.11, saving to mysql db.

I am trying to update the "chat_user_status" field for my "Teacher" model, which extends the abstract User model.

Model.py code for Teacher:

class TeacherProfile(models.Model):
    teacher = models.OneToOneField(
        UserProfile, related_name='teacher', primary_key=True, parent_link=True, on_delete=models.CASCADE)
    chat_user_status = models.BooleanField(default=False, verbose_name='Chat Status')

    def save(self, *args, **kwargs):
            identity = self.teacher.email
            friendlyName = self.teacher.first_name + \
                ' ' + self.teacher.last_name
    
            super(TeacherProfile, self).save(*args, **kwargs)

Views.py (Teacher model) login code that is trying to update the chat_user_status field, and nothing is updating, and no error:

def login_user(request):
    logout(request)
    email = password = ''

    if request.POST:
        email = request.POST['email']
        password = request.POST['password']

        user = authenticate(username=email, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                if is_teacher(user):
                    user.chat_user_status=True
                    user.save()

Views.py relevant code from my chat app that is trying to save the data

def update_chatuser_status(uid):
    user_profile = UserProfile.objects.get(id=uid)
    teacher_profile = None

    try:
        teacher_profile = TeacherProfile.objects.get(teacher_id=user_profile.id)
    except:
        pass

    if teacher_profile != None:
        teacher_profile['chat_user_status']=False
        teacher_profile.save()

There is no change to the field 'chat_user_status' after calling save(). There is no error message. I have searched other answers, either they don't work, or are irrelevant.

Share Improve this question asked Nov 21, 2024 at 4:30 Lawrence DeSouzaLawrence DeSouza 1,0215 gold badges17 silver badges37 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

As far as I understand, you have two seperate errors that result in the field not being updated.

In login_user() you are operating on a UserProfile Object which does not have the field chat_user_status. You need to instead access the connected TeacherProfile and save that with the new property:

if is_teacher(user):
    user.teacherprofile.chat_user_status=True
    user.teacherprofile.save()

in update_chatuser_status you access the property like a dict key, which does not work. Instead you should access the field like so:

if teacher_profile != None:
        teacher_profile.chat_user_status=False
        teacher_profile.save()

本文标签: djangoUnable to save user model with new dataStack Overflow