0

This error is showing while login to admin panel " 'User' object has no attribute 'profile' "

 from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

# Create your models here.

class Student(models.Model):
  user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='student')

  age = models.IntegerField(max_length=2)
  standard = models.CharField(max_length=15)

  def __str__(self):
    return self.user.username

@receiver(post_save, sender=User)
def create_student_profile(sender, instance, created, **kwargs):
    if created:
        Student.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_student_profile(sender, instance, **kwargs):
    instance.profile.save()
    

subject_choices = [
  ('math','math'),
  ('science','science'),
  ('physics','physics'),
]

class Teacher(models.Model):
  user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='teacher')

  subject = models.CharField(max_length=20, choices=subject_choices)

  def __str__(self):
    return self.user.username

@receiver(post_save, sender=User)
def create_teacher_profile(sender, instance, created, **kwargs):
    if created:
        Teacher.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_teacher_profile(sender, instance, **kwargs):
    instance.profile.save()

batch_choices = [
  ('10','10:00 AM'),
  ('12','12:00 PM'),
  ('02','02:00 PM'),
  ('04','04:00 PM')
]

class Batch(models.Model):
  b_name = models.CharField(max_length=20)
  b_time = models.CharField(max_length=10, choices=batch_choices)

also I want that whenever a student registers, a batch is created named by the time field in it and on every 41st student, creates another batch having the next time choice

Altaf Husain
  • 51
  • 1
  • 1
  • resolved in @receiver(post_save, sender=User) def create_student_profile(sender, instance, created, **kwargs): if created: Student.objects.create(user=instance) sender=Student should be used and in other sender=Teacher should be used – Altaf Husain Nov 16 '21 at 09:59
  • I want to create a Batch object everytime a student registers and join the batch automatically and every 41st student creates a new batch and joins it – Altaf Husain Nov 16 '21 at 10:01
  • `instance.teacher.save()` instead of `instance.profile.save()`. – Willem Van Onsem Nov 16 '21 at 11:15
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 22 '21 at 02:34

0 Answers0