Home > Software design >  Dangjo with Mongodb, spaces in field name: FieldError: Cannot resolve keyword '' into fiel
Dangjo with Mongodb, spaces in field name: FieldError: Cannot resolve keyword '' into fiel

Time:09-22

Short-time lurker looking for a bit of guidance. I started a small project to automate a little of the day-to-day. I chose Python and from here I have become interested in programming. About a month ago I decided to keep track of what was going on and hooked up a MongoDB instance to the automated process. Moving onto a GUI using Django. I have hit an issue with one of my views and I suspect the answer will likely be to remove spaces from field names but thought I would ask here as a hail mary :)

When trying to apply a filter to my objects it returns the following error:

Error:
django.core.exceptions.FieldError: Cannot resolve keyword '' into field. Choices are: NP Title ID, Product Code, Product Type, _id

Problem model and view below:

Model:
    class Railway_Products(models.Model):
        _id = models.ObjectIdField(name='_id', editable=False)
        np_title_id = models.CharField(name='NP Title ID', max_length=15)
        product_type = models.CharField(name='Product Type', max_length=15)
        product_code = models.CharField(name='Product Code', max_length=15)

class Meta:
    db_table = "Products"
    verbose_name_plural = "products"

View:
    class Railway_Products_ListView(generic.ListView):
    model = Railway_Products
    context_object_name = 'product_list'
    queryset = model.objects.filter(name='NP Title ID',__icontains='PPSA')

I know the issue is this line:

queryset = model.objects.filter(name='NP Title ID',__icontains='PPSA')

and name= not enjoying the '' or simply not being accepted. I have tried assigning 'NP Title ID' to a variable. Which just results in Cannot resolve variablename into the field. As a quick test, I renamed the field within the MongoDB, model and view to NPTitleID. As below:

queryset = model.objects.filter(NPTitleID__icontains='PPSA')

which works. However, I would like to avoid renaming field names within collections as these have all been set up with spaces. Is there a way I can get this to work with the spaces intact?

UPDATE with @AKX answer below:

model:
np_title_id = models.CharField(name='NPTitleID', db_column='NP Title ID', max_length=15)

view:
queryset = model.objects.all().filter(NPTitleID__icontains='PPSA')

This returns the expected results to the view with no errors. Had to do a little rename on the admin side filters changing 'NP Title ID' to 'NPTitleID' but this was much more preferable to changing all field names within MongoDB

CodePudding user response:

Django doesn't support MongoDB for its ORM by default, so this is a bit of a shot in the dark.

If your Django/MongoDB adapter works like Django works by default, you can set db_column on each field to what the actual field is in the database, e.g.

np_title_id = models.CharField(
    max_length=15,
    db_column='NP Title ID',
)
  • Related