I have seen several similar posts of other attributes found but not this. New to Python and Django- I've done the first part of the several tutorials including Django's "Polls" tutorial and when it gets to the point where I syncdb for my app I invariably get 'AttributeError: 'module' object has no attribute CharField.
In models I have copied exactly as the tutorial says:
from django.db import models
class Poll(models.Model):
question = models.Charfield(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
# Create your models here.
'polls' is also added to installed apps and I'm using sqlite3, windows 7, python 2.7.
Any help very appreciated! (I'm trying very hard to learn!)
That is CharField, with uppercase 'f', and not Charfield as in your code.
I think in forms.py you are using
from django.forms import forms
Please use this
from django import forms
change charfield to:
CharField(max_length = 10)
both C and F should be capitalized
This :
question = models.CharField(max_length=200)
Instead of :
question = models.Charfield(max_length=200)
I have the same error but following code works for me:
from django.db import models
#Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=100)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
choice_text = models.CharField(max_length = 200)
votes = models.IntegerField(default =0)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
Simply change charfield() to charField() ..
In model Poll, spelling of CharField is not properly formatted. Ie you have written a small letter f inplace of a capital letter F. So, replace Charfield by CharField. You can see the code below:
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published') class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
Use this
from django.db import models
Related
In my app polls, we have the file models.py:
from __future__ import unicode_literals
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
And after starting a shell with python manage.py shell, we can write this:
from polls.models import Question
q = Question(question_text = "What's new?", pub_date=timezone.now())
The Question class is clearly taking constructor arguments when an object is created. But in our class definition of Question in models.py, there is no specification of any constructor arguments. So how does this work?
As Question class inherits models.Model class, it gets the constructor from there itself.
I have the following model (below).
I would like a query that returns every record of parent Question(models.Model), where ALL the values for the is_relevant field within the child Choice(models.Model) are "True".
Having a difficult time creating the QuerySet. Any Assistance would be very helpful.
model.py
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
is_relevant = models.BooleanField()
You can use the reverse relationship of the Question model like so:
Question.objects.filter(choice__is_relevant=True)
The choice in choice__is_relevant is the lowercase name of the model Choice.
Reference: Relationship lookups
So I am trying to create a custom Manager that extends the default one and I am getting an error that for the life of me I cannot fix. I've read all the django docs and can't see what I've done wrong!
ERROR:
NameError at /
name 'article_finish_date' is not defined
Here is my models.py
import datetime
from django.conf import settings
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class ArticleManager(models.Manager):
def get_queryset(self):
return super(ArticleManager, self).get_queryset().filter(article_finish_date==None)
class Article(models.Model):
article_name_text = models.CharField(max_length=200)
article_creation_date = models.DateTimeField('date created')
article_publish_date = models.DateTimeField('date published', null=True, blank=True)
article_finish_date = models.DateTimeField('date finished', null=True, blank=True)
def __str__(self):
return self.article_name_text
actives = ArticleManager()
I have tried filtering by all the different values of the Article model, however the same issue occurs. I have tried both migrated and makemigrations, however no progress has been made.
Many thanks in advance, ask if you need more details!
The error is in this part:
.filter(article_finish_date==None)
Python is trying to evaluate the expression article_finish_date==None, except article_finish_date hasn't been defined yet. Instead, you want to use a single =
.filter(article_finish_date=None)
I got this error when im coding in the second part of the django tutorial, i don't know why, i have the same code that the website.
Django 1.8.3
ERRORS:
<class 'polls.admin.ChoiceInline'>: (admin.E202) 'polls.Choice' has no ForeignKey to 'polls.Choice'.
System check identified 1 issue (0 silenced).
My models.py
import datetime
from django.db import models
from django.utils import timezone
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
My admin.py
from django.contrib import admin
from .models import Choice, Question
class ChoiceInline(admin.StackedInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None,{'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
admin.site.register(Choice, ChoiceInline)
admin.site.register(Question, QuestionAdmin)
I really will preciate the help, I really have no idea what is the problem , and want to finish this tutorial
The ChoiceInline has already been included in your QuestionAdmin by setting
inlines = [ChoiceInline].
This means that when you edit a question, you will be able to add, edit and delete that question's choices at the same time.
You are getting the error because of this line:
admin.site.register(Choice, ChoiceInline)
This is invalud, becayse you can't register a model with an Inline. You can only register a model with a ModelAdmin class. To stop the error, simply remove this line from your code.
If you want to edit choices by themselves, you need to define a ChoiceAdmin class and register that.
admin.site.register(Choice, ChoiceAdmin)
Or, if you don't need any customisation, you don't actually need a model admin.
admin.site.register(Choice)
For example i have this code
app/models.py
from django.db import models
class Poll(models.Model):
title = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Question(models.Model):
poll = models.ForeignKey(Poll)
question_text = models.CharField(max_length=200)
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
At first, I can add in Django Admin Poll, next i can add some question and answers to question, but i can do it only after creating poll and i can add only one question and answers to it at first time. How i can do it in one page while creating or updating Poll and add more than one question+answers?