Re: How to populate a custom ModelForm?
Ok, thanks for your time. --~--~-~--~~~---~--~~ 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: How to populate a custom ModelForm?
On Thu, 2009-02-26 at 09:53 -0800, rajeesh wrote: > Sorry for repeating this, but I'm still clue-less on it and the > deadline approaches fast: The current work_around(http://dpaste.com/ > hold/427) seems too ugly. Let's be clear: If you have a solution and a deadline, then you're set. You have *a* solution. You can work on making it cleaner as time goes by. It's no longer a showstopper. I realise you have a particular problem you're trying to solve, but part of the reason I, for one, haven't continued responding is because it's a problem that seems to involve a large amount of work even to understand and I'm not really sure what you're trying to do, even with your short example. > But that's not why I'm back. I've stumbled > upon one more similar problem. Both can be grouped together into this > statement: What's the right approach in saving a ModelForm when it > contains a subset of fields of a related_field or an inline_formset > formed by such a subset? Hardly an amazingly clear question. The answer could be "don't do that" -- see the next paragraph for details. I suspect the general answer to this (and this is where I've made a mental note to approach things from if I get time and inclination to look at this more) is that you've gone beyond what a ModelForm is intended to do. ModelForms wrap a particular style of usage. For more specific customisations, subclass Form. You can do anything with a Form subclass (including adding a save() method to it so that it saves thing). I strongly suspect you're at the point where the "neat" solution to your problem is going to be writing the form out field-by-field and manipulating the data directly. In my own form usage, I find I relatively rarely use ModelForm, because most of my "pages" do not correspond one-to-one with the backing model storage. So I regularly use Form subclasses and then manipulate the data into the models that save them. Your case, using the admin, requires you only to match the ModelForm API (mostly the save() method) so that the admin can use it. But I don't think you're necessarily required to use a ModelForm. My only uncertainty here is that I haven't explicitly checked the admin interface isn't making an assumption there (if it was, I'd need to be convinced with good arguments that that wasn't a semi-bug in the admin). Or maybe you're just trying to stretch the admin beyond what it can naturally do. If that's the case, then it's quite possible that any solution is going to be semi-complex. Again, you have *a* solution, so that's certainly a starting point. Maybe that gives you some ideas for alternative avenues of investigation. Remember that you're in heavy customisation territory here, so writing some code and understanding the underlying structures is pretty much compulsory. THere isn't going to be a one-line solution. Regards, Malcolm --~--~-~--~~~---~--~~ 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: How to populate a custom ModelForm?
Sorry for repeating this, but I'm still clue-less on it and the deadline approaches fast: The current work_around(http://dpaste.com/ hold/427) seems too ugly. But that's not why I'm back. I've stumbled upon one more similar problem. Both can be grouped together into this statement: What's the right approach in saving a ModelForm when it contains a subset of fields of a related_field or an inline_formset formed by such a subset? All those excluded arguments are required and need to be attached to the related_instance at some_point. A Note at the documentation, http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form seems to identify the problem but offers a trivial solution at api level. --~--~-~--~~~---~--~~ 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: How to populate a custom ModelForm?
Pasting the code here to check any further problem ;) ## I've a model like this: class Receipt(models.Model): r_number= models.IntegerField() transaction = models.OneToOneField(Transaction,editable=False) ## Transaction is from another app and there it is used as usual. But here, I don't ## want the user to edit it directly. We'll ask for a few attributes and will create ## our own object using those values plus some others calculated by code. So I'm ## giving out a custom_form as this: class ReceiptForm(forms.ModelForm): r_number= forms.IntegerField() r_date = forms.DateField() subscriber = forms.ModelChoiceField(queryset=Subscriber.objects.all ()) ## Now we're overriding get_form and save_model of the AdminClass. save_model ## calculates values for obj_attrs from the form before saving. get_form populates ## the form_fields from the obj values. class ReceiptAdmin(admin.ModelAdmin): form= ReceiptForm def save_model(self,request,obj,form,change): t_date = form.cleaned_data['r_date'] client = 'S%s' %(form.cleaned_data['subscriber'].id) if not change: obj.transaction = Transaction.objects.create (t_date=t_date, client=client) else: for attr in ('t_date','client',): setattr(obj.transaction,attr,eval(attr)) obj.transaction.save() obj.save() ## This far it's OK. Now to populate the custom_form fields from the object. def get_form(self,request,obj=None,**kwargs): form = super(ReceiptAdmin,self).get_form(request,obj,**kwargs) if obj is not None: ## Text-like inputs are filled as below. form.base_fields['r_date'].widget.attrs['value'] = obj.transaction.t_date client = obj.transaction.client.split('S')[-1] subscriber = Subscriber.objects.get(pk=int(client)) ## To populate select/choice fields? I'm going through a two-pass approach ## for now. Set its title with the value and later set the corresponding ## option using js. This is where I feel bad. form.base_fields['subscriber'].widget.attrs['title'] = subscriber.id return form --~--~-~--~~~---~--~~ 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: How to populate a custom ModelForm?
> I get "Item not found" when I go to that entry. > I don't know what happened. I'd checked the link many times yesterday. But 'not found' by today! Anyway, pasted once more to http://dpaste.com/hold/427/ . > That sounds like the normal use-case for Meta.exclude in the ModelForm. > Can you just use that? I use the Transaction model in many places in different ways. In the current_scenario, we don't expect the users to edit it directly. But in other places, they are supposed to. --~--~-~--~~~---~--~~ 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: How to populate a custom ModelForm?
On Sun, 2009-02-22 at 23:18 -0800, rajeesh wrote: > Sorry for not including the code before. I've dpasted it now: > http://dpaste.com/123850/ . > I get "Item not found" when I go to that entry. > Problem can be summarized as this: I've a model M with certain fields > and some foreign keys. I don't want the user to add/edit the FK > directly from M's ModelForm. That sounds like the normal use-case for Meta.exclude in the ModelForm. Can you just use that? Regards, Malcolm --~--~-~--~~~---~--~~ 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: How to populate a custom ModelForm?
Sorry for not including the code before. I've dpasted it now: http://dpaste.com/123850/ . Problem can be summarized as this: I've a model M with certain fields and some foreign keys. I don't want the user to add/edit the FK directly from M's ModelForm. Instead we'll do that in code from certain values provided by the user. For that we've a custom ModelForm and that custom_form will be attached to the M's ModelAdmin, overriding get_form() and save_model(). I want to know whether my approach is reliable and easiest. If yes, what's the best way to populate edit_form fields?(Not just initial values). --~--~-~--~~~---~--~~ 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: How to populate a custom ModelForm?
On Sat, 2009-02-21 at 20:38 -0800, rajeesh wrote: > > Well, Let me re-phrase that question: I'm currently using the > following line of code to populate text_input fields in the custom > form: > form.base_fields['r_date'].widget.attrs['value'] = > obj.transaction.trans_date > But is this acceptable way of coding? Where are you doing this? In the form's __init__ method? If so, don't ever touch base_fields, only work with self.fields. If you're only intending to populate the initial data for a particular form field, then usually you would do just that: self.fields[].initial = Or you could update the form's initial data dictionary: self.initial_data[] = ... When the field is rendered the initial data value will be passed through to the widget correctly. > If yes, how can I populate > ChoiceFields? Depends upon how you're using them. You've provided no details. How about spending five minutes to create a simple model and form demonstrating what you're trying to do? Malcolm --~--~-~--~~~---~--~~ 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: How to populate a custom ModelForm?
Well, Let me re-phrase that question: I'm currently using the following line of code to populate text_input fields in the custom form: form.base_fields['r_date'].widget.attrs['value'] = obj.transaction.trans_date But is this acceptable way of coding? If yes, how can I populate ChoiceFields? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---