Re: Generic views: how to use them properly?

2007-06-04 Thread Michael Radziej

On Fri, Jun 01, Sacher Khoudari wrote:

> can I add a book to an author? I mean, how do I call
> "django.views.generic.create_update.object_create" with "model=Book",
> and pass it the value for it's foreign key?

You're right, generic views are quite limited. And they work only with
"oldforms", which are due to be replaced with "newforms", so it isn't really
worth improving. 

I personally am still with oldforms, and I like a programming style where I
don't write all the form handling code into my custom views. Instead, I've
patched the original generic views to accept a custom manipulator. With
this, I handle everything special (and also your case) within the
manipulator. Additional data like your foreign key would be passed to the
__init__() function of the manipulator.

Michael


-- 
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100
http://www.noris.de - The IT-Outsourcing Company
 
Vorstand: Ingo Kraupa (Vorsitzender), Joachim Astel, Hansjochen Klenk - 
Vorsitzender des Aufsichtsrats: Stefan Schnabel - AG Nürnberg HRB 17689

--~--~-~--~~~---~--~~
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: Generic views: how to use them properly?

2007-06-02 Thread Nathaniel Whiteinge

The only argument that is `required for the create_object generic
view`__ is a model.

.. __:http://www.djangoproject.com/documentation/generic_views/#django-
views-generic-create-update-create-object

I've run into this problem a few times when I was getting started. It
took me a while to get acclimated to reading the built-in
documentation for some reason, but most of the info I've ever needed
turned out to be in there. :-P

I'm afraid I've never used the create and update generic views, so I
can't warn you about any pitfalls. The reason I've never used them is
they haven't been updated for Django's newforms yet--so use caution,
things may change when they're updated.

Good luck.
- whiteinge


--~--~-~--~~~---~--~~
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: Generic views: how to use them properly?

2007-06-02 Thread Amit Ramon

Hi,

I'm quiet new to django myself, so this may not be the definitive answer, but 
perhaps it would help.

I've done something similar, but I created my own view rather than using the 
generic view, and used a form to create the dependent object (the book in 
your case). I used the method forms.form_for_model to create a form based on 
the model, which I guess should be similar to what the generic create view 
does. When displaying the form returned by this method, a drop-down box was 
displayed for selecting the parent object (your Author), allowing the user to 
select the author.

Have you tried to not pass the author, and then saw what you get? It's a 
speculation, but it may work for you as I described.

If you do want to force a specific author - i.e., not to allow the user to 
select it - it's something different, but can be done, at least if you use 
your own view. If you have questions about how to do it, just ask.

Cheers,

Amit

ביום שישי 01 יוני 2007, 23:11, נכתב על ידי Sacher Khoudari:
> 
> Hello!
> 
> I'm new to Django, and are currently playing around a bit. I've
> finished the tutorial, and have just started a small sample app. I
> really like Django. Although its different to everything I have done
> before (I confess, it was only PHP, I don't know Rails, Tomcat, Zope
> or anything else), you can get into it very quickly. The tutorial is
> great, and I think I'm understanding the design philosophies.
> 
> But, I have some trouble with the generic views. They are great,
> really, but either they lack a simple feature I think they should
> have, or they lack some proper documentation, or I'm a idiot and don't
> know how to use Google.
> 
> Ok, given the following situation: I have two models, one depends on
> the other by a foreign key. Call them
> 
> class Author:
> name = models.CharField(maxlength=32)
> 
> class Book:
> author = models.ForeignKey( Author )
> 
> Now I can get a list of all authors in my database, a list of all
> books by author, details for each book, I can edit and delete each
> book... that works all fine. And, it's really easy to do that. But how
> can I add a book to an author? I mean, how do I call
> "django.views.generic.create_update.object_create" with "model=Book",
> and pass it the value for it's foreign key?
> 
> Here is the URL pattern I want to use:
> 
> urlpatterns = patterns('django.views.generic.create_update',
>  (r'^authors/(?P\w+)/add_book/$', 'object_create',
> dict(model=Book,slug_field='name')),
>  )
> 
> Ok, instead of the author's name (as slug) y could have used it's ID.
> But that doesn't matter. Anyway, doing this I get an error. I don't
> have the error message here right now (I'm sorry, I'm developing/
> playing around at home, but I don't have an internet access there, so
> I'm on another computer), but it tells me, that object_create does not
> expect this "slug" field. "object_id" doesn't work either. The
> documentation (even the open book) doesn't tell me anything about
> that. Google doesn't find much if I search for "object_create".
> 
> Any ideas/hints/solutions? Is this a missing feature? Or does Django
> lack this feature by intention? Or have I missed something? I don't
> want to teach you in philosophy or design goals, but I think this
> should be part of the generic views' features. It's a further simple
> (generic) action I would like to make use of.
> 
> Thanks!
> Sacher
> 
> 
> > 
> 

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