[Answered ]-Choice_set django tutorial part one

2๐Ÿ‘

โœ…

I got it to work. I added another level to my database in the settings.py:

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': '/home/JoeButy/mysite/polls/db.sqlite',
    'USER': '',
    'PASSWORD': '',
    'HOST': '',
    'PORT': ''
    }
}

And this was the install bit I have:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'mysite.polls',
    'mysite.myapp',
)

Thanks for the help. I hope someone else can learn from this! cheers!

Here is the entire appendix Code:

06:15 ~ $ cd mysite                                                   
06:16 ~/mysite $ python ./manage.py syncdb                            
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table polls_poll
Creating table polls_choice

You just installed Django's auth system, which means you don't have an
y superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'joebuty'): 
E-mail address: joebuty@gmail.com
Password: 
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
No fixtures found.
06:17 ~/mysite $ python ./manage.py sql polls
BEGIN;
CREATE TABLE "polls_poll" (
    "id" integer NOT NULL PRIMARY KEY,
    "question" varchar(200) NOT NULL,
    "pub_date" datetime NOT NULL
)                                                                     
;                                                                     
CREATE TABLE "polls_choice" (
    "id" integer NOT NULL PRIMARY KEY,
    "poll_id" integer NOT NULL REFERENCES "polls_poll" ("id"),
    "choice" varchar(200) NOT NULL,
    "votes" integer NOT NULL
)                                                                     
;                                                                     
COMMIT;
06:17 ~/mysite $ python ./manage.py shell                             
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
Type "copyright", "credits" or "license" for more information.

IPython 1.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from polls.models import Poll, Choice

In [2]: p=Poll.objects.get(pk=1)
----------------------------------------------------------------------
-----                                                                 
DoesNotExist                              Traceback (most recent call 
last)                                                                 
/usr/local/lib/python2.7/dist-packages/django/core/management/commands
/shell.py in <module>()                                               
----> 1 p=Poll.objects.get(pk=1)

/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py in 
get(self, *args, **kwargs)
    130
    131     def get(self, *args, **kwargs):
--> 132         return self.get_query_set().get(*args, **kwargs)
    133
    134     def get_or_create(self, **kwargs):

/usr/local/lib/python2.7/dist-packages/django/db/models/query.py in ge
t(self, *args, **kwargs)
    347         if not num:
    348             raise self.model.DoesNotExist("%s matching query d
oes not exist."                                                       
--> 349                     % self.model._meta.object_name)
    350         raise self.model.MultipleObjectsReturned("get() return
ed more than one %s -- it returned %s! Lookup parameters were %s"     
    351                 % (self.model._meta.object_name, num, kwargs))

DoesNotExist: Poll matching query does not exist.

In [3]: p=Poll.objects.all()                                          

In [4]: print p
[]                                                                    

In [5]: p =Poll(question="What''s up?", pub_date=datetime.datetime.now
())
----------------------------------------------------------------------
-----                                                                 
NameError                                 Traceback (most recent call 
last)                                                                 
/usr/local/lib/python2.7/dist-packages/django/core/management/commands
/shell.py in <module>()                                               
----> 1 p =Poll(question="What''s up?", pub_date=datetime.datetime.now
())                                                                   

NameError: name 'datetime' is not defined

In [6]: import datetime

In [7]: p =Poll(question="What''s up?", pub_date=datetime.datetime.now
())                                                                   

In [8]: print p
What''s up?                                                           

In [9]: p.save()

In [10]: p=Poll.objects.get(pk=1)

In [11]: p.was_published_today()
Out[11]: True

In [12]: p.choice_set.all()
Out[12]: []

In [13]: p.choice_set.create(choice="Not much", votes=0)
Out[13]: <Choice: Not much>

In [14]: p.choice_set.create(choice="The Sky", votes=0)               
Out[14]: <Choice: The Sky>

In [15]: p.choice_set.create(choice="Best Web App Ever!", votes=0)    
Out[15]: <Choice: Best Web App Ever!>

In [16]: c.poll
----------------------------------------------------------------------
-----                                                                 
NameError                                 Traceback (most recent call 
last)                                                                 
/usr/local/lib/python2.7/dist-packages/django/core/management/commands
/shell.py in <module>()                                               
----> 1 c.poll

NameError: name 'c' is not defined

In [17]: p.choice_set.all()
Out[17]: [<Choice: Not much>, <Choice: The Sky>, <Choice: Best Web App
 Ever!>]                                                              

In [18]: p.choice_set.count()                                         
Out[18]: 3

In [19]: Choice.objects.filter(poll__pub_date__year=2014)             
Out[19]: [<Choice: Not much>, <Choice: The Sky>, <Choice: Best Web App
 Ever!>]                                                              

In [20]: c.save()
----------------------------------------------------------------------
-----                                                                 
NameError                                 Traceback (most recent call 
last)                                                                 
/usr/local/lib/python2.7/dist-packages/django/core/management/commands
/shell.py in <module>()                                               
----> 1 c.save()

NameError: name 'c' is not defined

In [21]: 
๐Ÿ‘คReverend_Dude

Leave a comment