How do I pass a specific CustomUser to the class-based CreateView in Django?

92 Views Asked by At

After finishing a tutorial, I decided to convert the function-based views in the app to class-based views as a way to learn both better. But I got stumped with passing the fields of a CustomUser to the CreateView:

#model:
class CustomUser(AbstractUser):
    user_type_data=((1,"HOD"),(2,"Staff"),(3,"Student"))
    user_type=models.CharField(default=1, choices=user_type_data, max_length=10)

class Staff(models.Model):
    id=models.AutoField(primary_key=True)
    admin=models.OneToOneField(CustomUser, on_delete=models.CASCADE)
    email=models.EmailField()
    address=models.TextField(blank=True)
    objects=models.Manager()

#view:
@method_decorator(csrf_exempt, name='dispatch')
class SubjectCreateView(CreateView):
    model = Subject
    fields = ['id', ... ]

What I want to put in the fields= are: staff.first_name, staff.last_name, but none of the combinations I tried worked. This is my first encounter with CustomUser, so my searches weren't fruitful. Appreciate some pointers.

1

There are 1 best solutions below

0
On

What I post below doesn't solve my problem with CreateView, but if you are similarly stuck, this post offers one of the tips on how to display all the fields in ModelForms (fields = '__all__'). And this thread offers tips on how to get objects referenced as ForeignKey.

If someone can help explain how to set up a CreateView for a CustomUser with fields that aren't listed in models.py-but maybe coming out of AbstractUser or BaseUser, I'll accept it as the answer.