I have an overrided User model and a Cart model. I expect that a Cart model instance is created automatically once a User model instance is created. I am trying to pass the newly registered user into the get_queryset method, but no idea how to do it. Are there any other better ways of doing this? It's because I may need to do the same thing for other models unlike the User model which has a form that can pass values to the get_queryset method.
account/models.py:
class Cart(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
def __str__(self):
return self.user.email + '_cart'
account/views.py:
class RegisterView(generic.CreateView):
template_name = 'account/register.html'
form_class = RegisterForm
success_url = reverse_lazy('book:home')
def get_queryset(self):
sign_up = self.request.POST.get('register')
if sign_up:
c = Cart.objects.create(user=???)
c.save()
account/templates/account/register.html:
<form name="register" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Sign Up">
</form>