0

My Models.py, Here I'm using OneToOneField here, to extend StudentProfile.

from django.db import models 
from django.contrib.auth.models import User, UserManager


class SecretQuestion(models.Model):
    question = models.CharField(max_length=200)
    def __unicode__(self):
        return self.question


class StudentProfile(models.Model):
    user = models.OneToOneField(User)
    batch = models.CharField(max_length=10)
    course = models.CharField(max_length=20)
    date_of_birth = models.DateField()
    secret_question = models.ForeignKey(SecretQuestion)
    answer = models.CharField(max_length=20)
    contact = models.CharField(max_length=20)

And my registration view, I'm creating new user main_user and I'm using it to create Student Profile :-

def register_page(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            main_user = User.objects.create_user(
              username= form.cleaned_data['username'],
              password = form.cleaned_data['password1'],
             )
            user = StudentProfile.objects.create(               
                user_id = main_user.id,http://stackoverflow.com/questions/ask
                batch=form.cleaned_data['batch'],
                course=form.cleaned_data['course'],
                date_of_birth=form.cleaned_data['date_of_birth'],
                secret_question=form.cleaned_data['secret_question'],
                answer=form.cleaned_data['answer'],
                contact=form.cleaned_data['contact']
              )
        return HttpResponseRedirect('/register/success/')
    else:
       form = RegistrationForm()

   variables = RequestContext(request, {'form': form})
   return render_to_response('registration/register.html',variables)

After registering in Django, I'm getting,

FieldError at /register/

Cannot resolve keyword 'username' into field. Choices are: answer, batch, contact, course, date_of_birth, id, secret_question, user

This is happening after filling registration fields and pressing register button. I'm not able to interpret this error. What does this mean?

agf
  • 171,228
  • 44
  • 289
  • 238
JohnRK
  • 105
  • 1
  • 7

2 Answers2

1

You need to subclass RegistrationForm as described in Adding extra fields to django-registration form


User.objects.create_user has no keyword argument named username. The username must be the first positional argument:

        main_user = User.objects.create_user(form.cleaned_data['username'],
          password = form.cleaned_data['password1'],
         )

In the future, please include at least the line number where the error occurred.

Community
  • 1
  • 1
agf
  • 171,228
  • 44
  • 289
  • 238
  • I have done that, but still I'm getting same error. Even I don't know what is happening because if I remove whole username code (this part form.cleaned_data['username']) still I'm getting same error, any clue? – JohnRK Apr 10 '12 at 02:26
  • @Jainit Hmm see my edit, specifically the [second answer](http://stackoverflow.com/a/7580903/500584) to the linked question -- does that help? – agf Apr 10 '12 at 02:37
  • It seems that it will work, I have two questions, Is there any way to exclude email from RegistrationForm and when using class Meta and generating a form from a model, Is there any way to write quesryset in a field, I want to do something like this :- secret_question = forms.ModelChoiceField(queryset=SecretQuestion.objects.all()) – JohnRK Apr 10 '12 at 04:52
  • @Jainit You should be able to do `exclude = ('email',)` in the `Meta` of the form. You should be able to specify `secret_question` just like that in the form, even when using a model. See [Creating forms from models](https://docs.djangoproject.com/en/dev/topics/forms/modelforms/) for more info. – agf Apr 10 '12 at 05:03
  • Thanks a lot, Error fixed, I just extended my form with RegistrationForm, And it worked!, However, I needed to delete database and I have re-created it, It was not working without it – JohnRK Apr 10 '12 at 06:41
0

The current code shouldn't cause this error.

The error suggests you are filtering by a username keyword argument to your StudentProfile class, which does not have a username field according to your model.

The error should list which line your code is breaking on and tell you exactly where this is happening - that's where the answer lies. Your code? Django registration code? I don't know until you post it.

FYI, this error appears if you try to filter a model by an invalid field.

MyModel.objects.filter(non_existent_field=True) # would throw this error.

Passing an invalid field into create would cause a TypeError

Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • His code does causes this error, on the `form = RegistrationForm(request.POST)` line. It's sometimes confusing for people to get the line number with Django because they don't get a normal Python traceback. – agf Apr 10 '12 at 04:55