On 13.05.2008, at 06:32, RoMo wrote:

>
> I'm sorry for not understand what you were saying about the error with
> get if i have more than 1 post lol... kinda silly I'm sorry...
>
> Should I use .filter(Author=user)?

filter is used to retrieve many records, get is used to retrieve 1  
record.

if your are working with a ForeignKey on your Post model like so:

def Post(models.Model):
        name = CharField()
        author = model.ForgeinKey(Author)


and user is an object of type Author then this should give you all the  
Posts for one Author.

user.post_set.all()
and
user.post_set.count() will give you number of posts of the user.

all this information is in the docs at:
http://www.djangoproject.com/documentation/db-api/

specifically
http://www.djangoproject.com/documentation/db-api/#related-objects and 
http://www.djangoproject.com/documentation/db-api/#count

adi


>
>
> I tried with that and i get the next error:
>
> Incorrect integer value: '[<User: jorgeromo>]' for column 'Author_id'
> at row 1
>
> On May 12, 12:40 pm, Romo <[EMAIL PROTECTED]> wrote:
>> It works!!
>>
>> Thanks a lot for your help, Adi!
>>
>> cheers
>>
>> On May 12, 12:25 pm, Adi Jörg Sieker <[EMAIL PROTECTED]> wrote:
>>
>>> On 12.05.2008, at 18:57, RoMo wrote:
>>
>>>> Hello guys!
>>
>>>> First of all I don't know if I'm asking the right question, but  
>>>> i'll
>>>> try to explain myself the best I can:
>>
>>>> I would like to know if in a view there is a way that after a  
>>>> "try:"
>>>> and evaluate the expression as false, it justs ignores that query  
>>>> and
>>>> display a message error, but keep loading everything else.
>>
>>>> For example:
>>
>>>> try:
>>>>        posts = Post.objects.get(Author=user)
>>>>    except Record.DoesNotExist:
>>>>        //Missing code Here//
>>
>>>> What I would like to do is: check if user has posted something,  
>>>> if he
>>>> hasn't just displays a "you haven't posted anything yet" where the
>>>> Post should be and it keeps loading everything else normally.
>>
>>> what about:
>>
>>> try:
>>>         posts = Post.objects.get(Author=user)
>>>         # btw get will raise an AssertionError if more than record  
>>> is found.
>>>         # i.e. if a user posted more than one post
>>> except Record.DoesNotExist:
>>>         posts = None
>>
>>> return RequestContext(foo,bar, {'posts' : posts})
>>
>>> and in your template:
>>
>>> {%if posts %}
>>>         display the posts
>>> {% else %}
>>>         No haven't posted anything
>>> {% endif %}
>>
>>> regards
>>>    adi
>>
>>> --
>>> Adi J. Sieker         mobile: +49 - 178 - 88 5 88 13
>>> Freelance developer   skype:  adijsieker
>>> SAP-Consultant        web:    http://www.sieker.info/profile
>>>                        openbc:https://www.openbc.com/hp/
>>> AdiJoerg_Sieker/
> 
--
Adi J. Sieker         mobile: +49 - 178 - 88 5 88 13
Freelance developer   skype:  adijsieker
SAP-Consultant        web:    http://www.sieker.info/profile
                       openbc: https://www.openbc.com/hp/ 
AdiJoerg_Sieker/




--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to