On Feb 9, 7:05 pm, Russell Keith-Magee <[email protected]> wrote:
> On Mon, Feb 9, 2009 at 8:43 PM, jfmxl <[email protected]> wrote:
>
> > On Feb 9, 5:21 pm, Russell Keith-Magee <[email protected]> wrote:
> >> On Mon, Feb 9, 2009 at 6:58 PM, jfmxl <[email protected]> wrote:
> > Sorry, I am new to python and I guess you hadn't bothered to read the
> > elementary tutorial that I was following. I should have been more
> > specific:
>
> Specifics always help. And FYI: I have read the tutorial. I'm one of
> the core developers of Django, so I'm well aware of what the tutorial
> says.
I'm sorry. I didn't mean to accuse you of negligence at all! I just
meant that as an experienced python programmer you might have had a
different entry point to django than I did. I was unaware that you are
of the core. My apologies in any case.
> > So it was in response to that advice that I "saw he installation docs
> > for more information" and didn't find the relief spoken of and so
> > wrote here.
>
> > Thanks for your help, but I still am having a problem.
>
> Without seeing your code, its almost impossible to determine the cause
> of your problem. My initial guess would be that one of three things
> has happened:
>
> 1) You haven't actually saved the models.py file where you have
> defined the __unicode__() methods. An obvious error, but a
> surprisingly common one.
Yes. Just the sort of mistake I'm prone to make too. But not this
time.
> 2) You haven't restarted your python shell - you've just continued
> working with an old shell. You need to start a new Python shell
> session to force the models.py file to be reloaded.
I actually thought of that one myself after the first or second run.
> 3) You've misspelled __unicode__ in your models.py file. Most likely
> culprit here - using single underscores rather than double underscores
> in the method name. The method needs to be called _ _ unicode _ _
> (self). If you have used a different name, you won't see an error
> (since the alternate name will be a valid Python method name) but it
> won't generate the output you expect.
I've double-checked and that's not the case. I had cut and pasted the
code in question.
> If you post your example code as it currently stands, we may be able
> to provide more assistance.
I'm sorry it's so lengthy:
<console>
j...@ws0:~/src/python/django/mysite$ django-admin.py --version
1.1 pre-alpha SVN-9820
j...@ws0:~/src/python/django/mysite$ cat polls/models.py
from django.db import models
import datetime
# ...
# Create your models here.
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()
class Poll(models.Model):
# ...
def __unicode__(self):
return self.question
class Choice(models.Model):
# ...
def __unicode__(self):
return self.choice
class Poll(models.Model):
# ...
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
j...@ws0:~/src/python/django/mysite$ ./manage.py shell
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
Type "copyright", "credits" or "license" for more information.
IPython 0.8.4 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints
more.
In [1]: from mysite.polls.models import Poll, Choice
In [2]: Poll.objects.all()
Out[2]: [<Poll: Poll object>]
In [3]: Poll.objects.filter(id=1)
Out[3]: [<Poll: Poll object>]
In [4]: Poll.objects.filter(question__startswith='What')
Out[4]: [<Poll: Poll object>]
In [5]: Poll.objects.get(pub_date__year=2009)
Out[5]: <Poll: Poll object>
In [6]: Poll.objects.get(id=2)
---------------------------------------------------------------------------
DoesNotExist Traceback (most recent call
last)
/home/jfl/src/python/django/mysite/<ipython console> in <module>()
/usr/lib/python2.5/site-packages/django/db/models/manager.py in get
(self, *args, **kwargs)
91
92 def get(self, *args, **kwargs):
---> 93 return self.get_query_set().get(*args, **kwargs)
94
95 def get_or_create(self, **kwargs):
/usr/lib/python2.5/site-packages/django/db/models/query.py in get
(self, *args, **kwargs)
340 if not num:
341 raise self.model.DoesNotExist("%s matching query
does not exist."
--> 342 % self.model._meta.object_name)
343 raise self.model.MultipleObjectsReturned("get()
returned more than one %s -- it returned %s! Lookup parameters were
%s"
344 % (self.model._meta.object_name, num, kwargs))
DoesNotExist: Poll matching query does not exist.
In [7]: Poll.objects.get(pk=1)
Out[7]: <Poll: Poll object>
In [8]: p = Poll.objects.get(pk=1)
In [9]: p.was_published_today()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call
last)
/home/jfl/src/python/django/mysite/<ipython console> in <module>()
AttributeError: 'Poll' object has no attribute 'was_published_today'
In [10]: p = Poll.objects.get(pk=1)
In [11]: p.choice_set.create(choice='Not much', votes=0)
Out[11]: <Choice: Choice object>
In [12]: p.choice_set.create(choice='The sky', votes=0)
Out[12]: <Choice: Choice object>
In [13]: c = p.choice_set.create(choice='Just hacking again', votes=0)
In [14]:
In [15]: c.poll
Out[15]: <Poll: Poll object>
In [16]: p.choice_set.all()
Out[16]: [<Choice: Choice object>, <Choice: Choice object>, <Choice:
Choice object>]
In [17]: p.choice_set.count()
Out[17]: 3
In [18]: Choice.objects.filter(poll__pub_date__year=2009)
Out[18]: [<Choice: Choice object>, <Choice: Choice object>, <Choice:
Choice object>]
In [19]: c = p.choice_set.filter(choice__startswith='Just hacking')
In [20]: c.delete()
In [21]: p.choice_set.count()
Out[21]: 2
In [22]:
Do you really want to exit ([y]/n)? y
j...@ws0:~/src/python/django/mysite$
</console>
The response at [9] seems to indicate that none of the specifications
below the original first two seems to have been read by the
interpreter?
Thanks for taking the rime to help me.
>
> Yours,
> Russ Magee %-)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---