Re: Newbie problem with view/model interaction

2007-03-26 Thread Ross Burton

On Mar 25, 3:28 pm, "Ross Burton" <[EMAIL PROTECTED]> wrote:
> > What is it you want to do with the ones they've voted on vs. haven't? If
> > you're going to hide the ones they've already voted on, just write a
> > query in the view method that only returns those and pass that to the
> > context. (Note: the view method, not the template. See Ivan's response.)
>
> I thought I'd go this route -- it seemed simplier and more useful.
> However, I can't seem to work out the query.  My initial attempt:
>
> def pending(request):
> return object_list(request,
> Paper.objects.exclude(vote__user=request.user))
>
> However this appears to joining Paper and Vote, filtering out the rows
> where I voted, and then showing the rows remaining (which leads to
> duplicates).
>
> What would the correct filter be for all Papers which don't have a
> related Vote object with a given user field?

Ok so I'm a fool.  I now have this which works:

def pending(request):
all_papers = Paper.objects.all()
tuple_list = [p for p in all_papers if not
p.has_voted(request.user)]
return render_to_response('papers/paper_list.html',
{ 'object_list': tuple_list })

Would there be any theoretical advantage in using a generator to
filter the list of all papers as it is iterated?

Thanks,
Ross


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Newbie problem with view/model interaction

2007-03-25 Thread Ross Burton

(re-sending, apologies if this hits the list twice)

On Mar 24, 5:27 pm, "Todd O'Bryan" <[EMAIL PROTECTED]> wrote:
> What is it you want to do with the ones they've voted on vs. haven't? If
> you're going to hide the ones they've already voted on, just write a
> query in the view method that only returns those and pass that to the
> context. (Note: the view method, not the template. See Ivan's response.)

I thought I'd go this route -- it seemed simplier and more useful.
However, I can't seem to work out the query.  My initial attempt:

def pending(request):
return object_list(request,
Paper.objects.exclude(vote__user=request.user))

However this appears to joining Paper and Vote, filtering out the rows
where I voted, and then showing the rows remaining (which leads to
duplicates).

What would the correct filter be for all Papers which don't have a
related Vote object with a given user field?

Thanks,
Ross


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Newbie problem with view/model interaction

2007-03-24 Thread Ross Burton

Hi,

(I'm a bit of a newbie to Django so excuse any foolish mistakes)

I'm writing a basic paper review system for a conference (Paper is the
primary object, with Note and Vote objects having a foreign key to the
paper), and currently have it somewhat working.  Now my task is to
make the list of papers (currently a generic object_list view) show
whether the current user (all users are forced to login) has voted on
each paper.

My initial implementation was split across the template and the
model.  In my model:

class Paper (models.Model):
...
def has_voted(paper, user):
return paper.vote_set.filter(user__exact = user).count() != 0

then in the view:

{% for paper in object_list|dictsort:"title" %}
  {% if paper.has_voted( TODO ) %}


I then discovered that you can't pass arguments to methods in
templates, so I can't ask the model if the current user has voted.

Can anyone give any hints on how I can fix this?

Thanks,
Ross


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: Read only fields once created

2006-12-07 Thread Ross Burton

MerMer wrote:
> You can add "editable=False" to you database model.  This prevents it
> from showing up in Admin,  though of course it can still be edited
> directly in the DB.

This stops it being set in the admin when creating a new object, which
isn't what I want.

I think I'll have to hack the save() method...

Ross


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Programatic defaults

2006-12-05 Thread Ross Burton

Hi,

Is it possible to have programmatic defaults in a model?  If I have a
many to one relationship between A and B, I'd like A.flob to default to
B.flob.  Also A.foo should default to B.foo if it isn't specified.

Would the best way of doing this be in a custom save() method in the
model?

Thanks,
Ross


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Read only fields once created

2006-12-05 Thread Ross Burton

Hi,

Is it possible to have fields in the model that can be set at object
creation, but are immutable after that?  I have an "original estimate"
field that should only be set when creating the object, and never
edited after that.

Thanks,
Ross


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



Re: get_FOO_display and grouped data

2006-12-04 Thread Ross Burton

Jacob Kaplan-Moss wrote:
> Try::
>
> {% regroup feature.task_set.all|dictsortreversed:"priority" by
> get_priority_display as grouped %}
>
> That is, the "by" argument to {% regroup %} doesn't just have to be a field
> name; it can be anything that a variable could resolve.

Excellent, thanks.

Ross


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---



get_FOO_display and grouped data

2006-12-04 Thread Ross Burton

Hi,

I have a model with an integer field that uses choices for display
names:

class Task(models.Model):
PRIORITY_CHOICES = (
('0', 'Low'),
('1', 'Normal'),
('2', 'High')
)
priority = models.IntegerField(choices=PRIORITY_CHOICES)

In my view, I'm grouping the data on the priority:

{% regroup feature.task_set.all|dictsortreversed:"priority" by priority
as grouped %}
{% for group in grouped %}
  {{ group.grouper }}

However, when I do this, group.grouper expands to "0" or "2", not "Low"
or High".  How can I get the display name for this field?

Thanks,
Ross


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~--~~~~--~~--~--~---