views:

233

answers:

1

I'm trying to find some documentation of how to use the ForeignKeyRawIdWidget in my own forms. Currently I keep getting the error, "init() takes at least 2 non-keyword arguments (1 given)" which tells me nothing.

Any help would be most appreciated. Googling this turns up little but dev conversations and no examples that I can find of how to implement it.

Update: This is solved; the below code incorporates the solution.

class InvoiceForm(ModelForm):
    class Meta:
        model = Invoice
        widgets = {
            'customer': ForeignKeyRawIdWidget(Invoice._meta.get_field('customer').rel),
        }
A: 

This is from the source code (django.contrib.admin.widgets):

class ForeignKeyRawIdWidget(forms.TextInput):
    """
    A Widget for displaying ForeignKeys in the "raw_id" interface rather than
    in a <select> box.
    """
    def __init__(self, rel, attrs=None):
        self.rel = rel
        super(ForeignKeyRawIdWidget, self).__init__(attrs)

    #.....

From the remaining code, I would guess that rel is the foreign key field of your model. At one point, the code checks self.rel.limit_choices_to, and this attribute (limit_choices_to) can only be set on a ForgeinKey field.

Felix Kling
I reviewed this code as well... Not exactly the example or documentation I was hoping for! And it makes it very difficult to troubleshoot when you don't get anything at all.
tufelkinder