Re: Form/view for ManyToMany relationship

2011-03-05 Thread werefr0g



Le 05/03/2011 00:01, kgardenia42 a écrit :


Thanks.  This is pretty close to what I'm doing.  However, I'd like to
not have to write all the associated boiler-plate (i.e. I have to
figure out if any film/actor relationships have been deleted since the
last save and explicitly write code to remove those and so on).

Well, you can delete all related filmrole then save the new set, but I 
don't know if it's really an "efficient" way to handle that.


Out of topic: I have an obsession about providing "undo" ability as much 
as possible and showing changes before "committing" may help user too.

It seems that I would even have this problem if I added a simple "auto
now" date/time field to my relationship class, which seems a bit
restrictive.

Using a "timestamp" allows you to never edit / delete a entry: you just 
add new ones and work on last "timestamped" related entries for current 
data.


I use this myself intensively because I _need_ to provide audit tables. 
I use in this case two apps: one "normal" (without timestamps) and an 
"audit" app that is dependant of the "normal" app. I use two distinct 
tables (databases, actually) and performances are ok in my context 
(including about 30,000 modifications by batch at once at least once a 
week).

ModelForm already knows how to do all that (it can do so when the
relationship class is implicitly created) so what I imagined was
*somewhere* I could override a method and provide the defaults for the
missing fields and then it would all  just works magically.Does
this make sense?  Can anyone give me any pointers to how I might do
this (I'm keen to learn the "right way" to do things).

You can provide defaults in your Model. (I don't know if it's really 
required, but can use constants in these case: this allows to retrieve 
these entries without introducing more hardcoded values in your code)

I'm happy to hack this myself and contribute something if someone
could give me some pointers of where I should start or where something
like this ought to live.

I don't feel comfortable with your way to handle this relashionship, but 
it may be appropriate in a "wizard" approach. Maybe some form related 
project can appreciate your efforts.


Regards,

--
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Form/view for ManyToMany relationship

2011-03-04 Thread kgardenia42
On Fri, Mar 4, 2011 at 1:08 PM, werefr0g  wrote:
> On Thu, Mar 3, 2011 at 11:50 AM, werefr0g  wrote:
>
> Hello,
>
> Sorry if I misunderstand, but what is wrong about using a ModelForm on your
> Film bounded to the film instance?
>
> My bad... how did I missed the intermediary model involved in the ManyToMany
> relashionship. Sorry.
>
> When I tried that I kept running into this error:
>   "Cannot set values on a ManyToManyField which specifies an
> intermediary model.  Use foo.FilmRole's Manager instead.
>
> As you are using an intermediary model, you must insert multiple FilmRole,
> one for each actor to this film, but you don't want using a formset, do
> you?.
>
> Is the following going toward your goal?
>
> # forms.py
> class FilmActorsForm(forms.Form):
>     actors = forms.ModelMultipleChoiceField(queryset=Actors...,
> required=...)
>
> # views.py
> def assign_actors(film_id):
>     film = get_object_or_404(Film, id=film_id)
>     if request.method == 'POST':
>     form = FilmActorsForm(request.POST)
>     if form.is_valid():
>     chosen_actors = form.cleaned_data['actors']
>     current_actors = film.actors.all()
>     # handle deletion / creation of filmroles
>     return HttpResponseRedirect(...)
>     else:
>     form = FilmActorForm()

Thanks.  This is pretty close to what I'm doing.  However, I'd like to
not have to write all the associated boiler-plate (i.e. I have to
figure out if any film/actor relationships have been deleted since the
last save and explicitly write code to remove those and so on).

It seems that I would even have this problem if I added a simple "auto
now" date/time field to my relationship class, which seems a bit
restrictive.

ModelForm already knows how to do all that (it can do so when the
relationship class is implicitly created) so what I imagined was
*somewhere* I could override a method and provide the defaults for the
missing fields and then it would all  just works magically.Does
this make sense?  Can anyone give me any pointers to how I might do
this (I'm keen to learn the "right way" to do things).

I'm happy to hack this myself and contribute something if someone
could give me some pointers of where I should start or where something
like this ought to live.

Thanks.

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Form/view for ManyToMany relationship

2011-03-04 Thread werefr0g

sorry, I failed to pass the request to the view's function,..

Le 04/03/2011 22:08, werefr0g a écrit :

On Thu, Mar 3, 2011 at 11:50 AM, werefr0g  wrote:

Hello,

Sorry if I misunderstand, but what is wrong about using a ModelForm on your
Film bounded to the film instance?
My bad... how did I missed the intermediary model involved in the 
ManyToMany relashionship. Sorry.



When I tried that I kept running into this error:
   "Cannot set values on a ManyToManyField which specifies an
intermediary model.  Use foo.FilmRole's Manager instead.
As you are using an intermediary model, you must insert multiple 
FilmRole, one for each actor to this film, but you don't want using a 
formset, do you?.


Is the following going toward your goal?

# forms.py
class FilmActorsForm(forms.Form):
actors = forms.ModelMultipleChoiceField(queryset=Actors..., 
required=...)


# views.py
def assign_actors(film_id):
film = get_object_or_404(Film, id=film_id)
if request.method == 'POST':
form = FilmActorsForm(request.POST)
if form.is_valid():
chosen_actors = form.cleaned_data['actors']
current_actors = film.actors.all()
# handle deletion / creation of filmroles
return HttpResponseRedirect(...)
else:
form = FilmActorForm()

Regards,

--
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


--
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Form/view for ManyToMany relationship

2011-03-04 Thread werefr0g

On Thu, Mar 3, 2011 at 11:50 AM, werefr0g  wrote:

Hello,

Sorry if I misunderstand, but what is wrong about using a ModelForm on your
Film bounded to the film instance?
My bad... how did I missed the intermediary model involved in the 
ManyToMany relashionship. Sorry.



When I tried that I kept running into this error:
   "Cannot set values on a ManyToManyField which specifies an
intermediary model.  Use foo.FilmRole's Manager instead.
As you are using an intermediary model, you must insert multiple 
FilmRole, one for each actor to this film, but you don't want using a 
formset, do you?.


Is the following going toward your goal?

# forms.py
class FilmActorsForm(forms.Form):
actors = forms.ModelMultipleChoiceField(queryset=Actors..., 
required=...)


# views.py
def assign_actors(film_id):
film = get_object_or_404(Film, id=film_id)
if request.method == 'POST':
form = FilmActorsForm(request.POST)
if form.is_valid():
chosen_actors = form.cleaned_data['actors']
current_actors = film.actors.all()
# handle deletion / creation of filmroles
return HttpResponseRedirect(...)
else:
form = FilmActorForm()

Regards,

--
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Form/view for ManyToMany relationship

2011-03-03 Thread kgardenia42
On Thu, Mar 3, 2011 at 11:50 AM, werefr0g  wrote:
> Hello,
>
> Sorry if I misunderstand, but what is wrong about using a ModelForm on your
> Film bounded to the film instance?

When I tried that I kept running into this error:
  "Cannot set values on a ManyToManyField which specifies an
intermediary model.  Use foo.FilmRole's Manager instead."

... which led me down a different path but perhaps I should have
persevered more.

I have googled this error extensively and spent some time digging into
Django internals to figure out what is happening.  Basically I can see
that if Django didn't auto-generate the relationship manager class
then it will throw this error.

Conceptually I totally get why that is (it has no way of knowing what
values to assign to the extra fields).  What I don't get is how I'm
supposed to get around it.  Lets say I wanted to put some code
somewhere to munge the missing fields to some default value.  Where
would I put that code?

I can't relate to the error message at all.  I read about manager
classes and how I can override query sets and so on.  Sure that
totally makes sense.  But how does that help me here?How can I use
the manager for FilmRole?  Do I need to override some method in my
manager?  Which method?   An example would be really useful here.

Thanks.

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Form/view for ManyToMany relationship

2011-03-03 Thread werefr0g

Hello,

Sorry if I misunderstand, but what is wrong about using a ModelForm on 
your Film bounded to the film instance?


Regards

--
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.