'CharField' object has no attribute 'model'

1,242 views
Skip to first unread message

Billu

unread,
Dec 26, 2015, 1:34:02 AM12/26/15
to Django users
I'm trying to make a single choice quiz (i.e. Choose one from 4 options). I'll populate the quiz (with 30 questions) depending upon what topic the user chooses. I'm made some models and forms, but when I try to migrate it says 

AttributeError: 'CharField' object has no attribute 'model'

I don't even know which model has problem. I've tried it in Django 1.9 (with virtualenv) & 1.8, same error. I've tried both manage.py check & manage.py runserver and they all come up ok (with runserver saying I need to migrate). 

Python 2.7
Windows 7

Below are my models. Tell me if you require anything else. It's only partially made and I've taken long-cuts in many things, 1st I've to make the thing work.

from django.db import models
from django.template.defaultfilters import slugify
from django.conf import settings
from django.contrib.auth.models import User


class UserProfile(models.Model):
 user
= models.OneToOneField(settings.AUTH_USER_MODEL)
 topic
= models.CharField(max_length=50, blank=True)


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


class Subject(models.Model):
 subject_name
= models.CharField(max_length=50)


 
def __unicode__(self):
 
return self.subject_name


class QuestionAnswer(models.Model):
 subject
= models.ManyToManyField(Subject)
 question_text
= models.TextField(max_length=150)
 option_w
= models.CharField(max_length=75, blank=True, null=True)
 option_x
= models.CharField(max_length=75, blank=True, null=True)
 option_y
= models.CharField(max_length=75, blank=True)
 option_z
= models.CharField(max_length=75, blank=True)
 correct_answer
= models.CharField(max_length=75, blank=False, default=None)
 on
= models.BooleanField(default=True)


 
def __unicode__(self):
 
return self.id


 
class Meta:
 app_label
= 'QuestionAnswer'
 verbose_name
='Question'
 verbose_name_plural
= 'Questions'


class QuizAttempt(models.Model):
 username
= models.ManyToManyField(User)
 subject
= models.ManyToManyField(Subject)
 slug
= models.SlugField(unique=False)
 datetime
= models.DateTimeField(auto_now_add=True)
 questions_included
= models.ForeignKey(models.QuestionAnswer)
 correctly_answered_questions
= models.IntegerField()
 total_marks
= models.IntegerField()


 
class Meta:
 verbose_name
='Quiz'
 verbose_name_plural
= 'Quizzes'


 
def save(self, *args, **kwargs):
 
self.slug = slugify(self.username +' ' +  self.subject + ' ' + self.datetime.month +' ' +  self.datetime.year)
 
super(QuizAttempt, self).save(*args, **kwargs)


 
@property
 
def correct_answer_count(self):
   
return self._correct_answer_count

 
def __unicode__(self):
 
return "Quiz ID is {}".format(self.id)
 
return "The quiz was taken by {} on {}".format(self.username, self.topic)
 
return "The quiz was taken on {}".format(self.datetime)



Daniel Roseman

unread,
Dec 26, 2015, 9:20:52 AM12/26/15
to Django users
On Saturday, 26 December 2015 06:34:02 UTC, Billu wrote:
I'm trying to make a single choice quiz (i.e. Choose one from 4 options). I'll populate the quiz (with 30 questions) depending upon what topic the user chooses. I'm made some models and forms, but when I try to migrate it says 

AttributeError: 'CharField' object has no attribute 'model'

I don't even know which model has problem. I've tried it in Django 1.9 (with virtualenv) & 1.8, same error. I've tried both manage.py check & manage.py runserver and they all come up ok (with runserver saying I need to migrate). 


You'll need to show code of the migration that triggers the problem.
-- 
DR. 

Simon Charette

unread,
Dec 26, 2015, 9:40:52 AM12/26/15
to Django users
Hi Billlu,

Does one your model use an ArrayField?

Luis Zárate

unread,
Dec 28, 2015, 6:17:07 PM12/28/15
to django...@googlegroups.com
I guest the problem is here
questions_included = models.ForeignKey(models.QuestionAnswer)

Django models haven't QuestionAnswer.
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4fab10f3-8b29-4a1d-830a-26ff6ad6d0f5%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
"La utopía sirve para caminar" Fernando Birri



Billu

unread,
Dec 28, 2015, 11:57:06 PM12/28/15
to Django users
Thanks, I had changed it to 

questions_included = models.ForeignKey('quiz.Question', null=True)

and also changed the db from Postgresql to Sqlite. Will see if this problem again comes up.

@luisza14 - did you find out by going through the code or are there any other ways to find out?

Luis Zárate

unread,
Dec 29, 2015, 11:34:22 AM12/29/15
to django...@googlegroups.com
I only read the code.

I don't know if you refactorize your code, but based in the code above, I think is like this:

class QuestionAnswer(models.Model):
  ...


class QuizAttempt(models.Model):
 ...
 questions_included = models.ForeignKey(QuestionAnswer)



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages