Silently continuing past an exception: is this proper? how else can I do this?

19 views
Skip to first unread message

Andrew Chiw

unread,
Oct 23, 2016, 5:24:28 AM10/23/16
to Django users
I've got my models setup in Django where a Document is a model that can be related to a Transaction.
Now I'd like to make a DocumentForm with a dropdown of Transactions owned by the current Client, so that a client can say that this Document is related to this Transaction.

So I wrote some code that looks like this:
class DocumentForm(forms.ModelForm):
    path
= forms.FileField(label='Choose a file')
    description
= forms.CharField(label='What is it?')
    related_to
= forms.ModelChoiceField(queryset=Transaction.objects.none())

   
class Meta:
        model
= Document
        fields
= ['description', 'path', 'related_to']

   
def __init__(self, *args, **kwargs):
       
super(DocumentForm, self).__init__(*args, **kwargs)
       
self.fields["related_to"].queryset = Transaction.objects.filter(client=self.data.client)

So now I can do:
d = Document()
DocumentForm(d)

and the related_to field in this DocumentForm will automatically be populated by Transactions owned by the current client.

The problem is, when Document() isn't yet saved to the db, it isn't associated with a client. So DocumentForm's self.data.client will throw an exception, and I can't test for it with if.

So I'm thinking of just using try/except and doing nothing on except.

Is there a better way to do this? I heard that you shouldn't use exceptions for control flow, but here it looks like I am.

Andrew Chiw

unread,
Oct 23, 2016, 8:39:19 AM10/23/16
to Django users
It seems there's a problem with my code, because you're just supposed to pass a dict to DocumentForm (or any modelForm really), not a model instance.

I actually want to make a DocumentForm initialized with values from an existing Document, so I can list existing Documents and provide a way to re-upload them and edit their description. Is this the right way to do it? Or should I only create DocumentForms only for new Documents to be created?

Tom Evans

unread,
Oct 26, 2016, 6:48:41 AM10/26/16
to django...@googlegroups.com
On Sun, Oct 23, 2016 at 1:39 PM, Andrew Chiw
<randomshi...@gmail.com> wrote:
> It seems there's a problem with my code, because you're just supposed to
> pass a dict to DocumentForm (or any modelForm really), not a model instance.
>
> I actually want to make a DocumentForm initialized with values from an
> existing Document, so I can list existing Documents and provide a way to
> re-upload them and edit their description. Is this the right way to do it?

DocumentForm(instance=d)

I can't see anywhere in the docs where this is explicitly specified as
the stock ModelForm API, but it is firmly implied by this page:

https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/#modelform

The first positional argument is expected to be data submitted by the browser.

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages