1

I am trying to implement manytomany field relation in django-nonrel on mongodb. It was suggessted at to:

Django-nonrel form field for ListField

Following the accepted answer

models.py

class MyClass(models.Model):
    field = ListField(models.ForeignKey(AnotherClass))

i am not sure where the following goes, it has been tested in fields.py, widgets,py, models.py

class ModelListField(ListField):
    def formfield(self, **kwargs):
    return FormListField(**kwargs)

class ListFieldWidget(SelectMultiple):
    pass

class FormListField(MultipleChoiceField):
    """
    This is a custom form field that can display a ModelListField as a Multiple Select GUI element.
    """
    widget = ListFieldWidget

    def clean(self, value):
    #TODO: clean your data in whatever way is correct in your case and return cleaned data instead of just the value
    return value

admin.py

class MyClassAdmin(admin.ModelAdmin):
    form = MyClassForm

    def __init__(self, model, admin_site):
    super(MyClassAdmin,self).__init__(model, admin_site)

admin.site.register(MyClass, MyClassAdmin)

The following Errors keep popping up:

If the middle custom class code is used in models.py

name 'SelectMultiple' is not defined

If custom class code is taken off models.py:

No form field implemented for <class 'djangotoolbox.fields.ListField'>
Community
  • 1
  • 1
bobsr
  • 3,879
  • 8
  • 30
  • 37

1 Answers1

0

You just need to import SelectMultiple by the sound of it. You can put the code in any of those three files, fields.py would make sense.

Since it's pretty usual to have:

from django import forms

at the top of your file already, you probably just want to edit the code below to:

# you'll have to work out how to import the Mongo ListField for yourself :)
class ModelListField(ListField):
    def formfield(self, **kwargs):
    return FormListField(**kwargs)

class ListFieldWidget(forms.SelectMultiple):
    pass

class FormListField(forms.MultipleChoiceField):
    """
    This is a custom form field that can display a ModelListField as a Multiple Select GUI element.
    """
    widget = ListFieldWidget

    def clean(self, value):
    #TODO: clean your data in whatever way is correct in your case and return cleaned data instead of just the value
    return value

You probably also want to try and learn a bit more about how python works, how to import modules etc.

Anentropic
  • 32,188
  • 12
  • 99
  • 147
  • When I am using this approach to define a custom model field, then the table generated corresponding to the model do not contain entry for 'custom field'. And I get exception like `Caught DatabaseError while rendering: no such column: MyTable.MyCustomField`. Am I missing something? – vivek.m Feb 21 '13 at 19:10
  • Figured out it's ignoring because of type 'ListField'. If I override function get_internal_type and return textField etc, then it's not ignored. Dont know why ListField is not working. – vivek.m Feb 21 '13 at 19:40
  • Had to override db_type and provide ListField as return type. ListField is not defined in data type for backend I am using, so db_type returned None and Django skipped that field. Now stuck at "Interface error : Error binding parameter 4 - probably unsupported type" for ListField, when trying to save the form! – vivek.m Feb 21 '13 at 19:52
  • I am using djangotoolbox. I asked a [question here](http://stackoverflow.com/q/15043969/333137) as I am confused in this for sometime now. thx. – vivek.m Feb 23 '13 at 18:28
  • ah, you have sqllite db? I thought from the original question you were on Mongo – Anentropic Feb 25 '13 at 10:44