yml schrieb: > Hello, > > what I am trying to do since this morning is to allow my logged in user > to create Members. As you will see below Member is related to the User > class by a foreignkey. > so far I create User, Member using generic view. What I would like to > do is to remove the user field from the form: "members_form". And this > is where I am stuck. > since the user attribute is mandatory for a member.
I'm on the magic-removal branch and I don't know if the following also applies to the released version--there is an undocumented optional `follow` parameter on the generic views. It can contain a dict of {fieldname: Boolean}; a True means to use this field, a False means to ignore it during validation and building the model from the form dict. You can then use a custom Manipulator to set the user field. To do this, create a version of the generic view that allows to pass in a manipulator class (I'll ask the developers to enhance generic views this way, since it's a really nice pattern. You can do pretty much with custom manipulators and short views that call the generic views. Here's how I use it in the view function: def follow_only(model_cls, field_list): """builds the "follow" dict that specifies which fields to take from the POST data. Manipulators are passed a dict { fieldname : Boolean } as `follow`. `follow_only()` builds this dict from the model class and a list of field names (containing the fields to take from the POST data. This should really go into a library module or to the django contributions. """ res = dict.fromkeys(model_cls._meta.get_follow().keys(), False) res.update(dict.fromkeys(field_list,True)) return res class MailboxAddManipulator(models.Person.AddManipulator): """Customized Manipulator""" def save(self, new_data): # add pwuse self.follow["pwuse"] = True new_data["pwuse"] = models.Person.get_pwuse_bitmask("mail") # add kunde self.follow["user"] = True new_data["user"] = get_new_mailbox_name(new_data["kunde"]) return models.Person.AddManipulator.save(self,new_data) def create_mailbox(request, admin_name): (...) models.Kunde.objects.managed_by(admin.id).distinct() ] ) return create_object( request, models.Person, follow=follow_only(models.Person,["name","pass_field","kunde"]), template_name="mailbox", post_save_redirect='..', manipulator_class=MailboxAddManipulator, ) Looking back at this, I don't think that you really need the `follow_only` function, you probably can pass follow={"unwanted_field": False} in your case. Let me know if this helped! I'm also interested in any comments about this style. Michael --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---