FieldDoesNotExist: ManyToManyField has no field named None
Asked Answered
C

5

13

I have two models in Django 1.8.8:

class Company(models.Model):
    name = models.CharField(max_length=200)
    members = models.ManyToManyField(User)
class Folder(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(null=True, blank=True)
    company = models.ForeignKey(Company, null=True, blank=True)
    parent = models.ForeignKey("Folder", null=True, blank=True)

and when I'm doing in template

{% for user in current_folder.company.members.all %}

I sometimes (randomly after a few page's reload) get very strange error:

FieldDoesNotExist: Company_members has no field named None

I also use sqlite3 database. Anyone have idea where is a problem?

Corkwood answered 15/3, 2016 at 9:12 Comment(7)
{% for user in current_folder.company.members.all %} {% if user %} ....Eerie
For extra context, I'm having the same issue - but only on travis-CI when running tests.Corroborant
And for completion, this is because I was running v1.8 of Django, not 1.8.xCorroborant
I am running into this problem as well, on a ManyToMany relation. I am trying to run some unit tests using the built in Django manage.py test tool in tandem with Flask.Bore
Same here working with django on a jupyter notebookIcaria
I suspect @AvinashRaj has it here ... your current_folder either has no company attached, or the company has no members in the instances where you are getting this error. If this is not the case, please post specific examples along with the data.Pashto
and why don;tyou try the latest LTS of django 2.2.x? 1.8 is gone long ago. or you are working on an old project?Lutherlutheran
L
1

This is most probbly the related django ticket you should check https://code.djangoproject.com/ticket/24513

And this issue could be somehow related though not 100% https://github.com/jet-admin/jet-django/issues/7

You might get some insight reading the threads.

Lutherlutheran answered 22/2, 2021 at 11:24 Comment(0)
D
0

If you want to access a ForeignKey field from an instance you can not access it directly as you did here

{% for user in current_folder.company.members.all %}

ForeignKey field is a company so it should be

current_folder.company_set()

Note: ForeignKey returns a set of objects. In your case a set of companies. That's why it returns FieldDoesNotExist

Disarrange answered 2/4, 2021 at 19:9 Comment(0)
N
0

Got this error for ManyToManyFields in Django 5 & IPython 8.23.0. I think it has to do with autoreload, because the error disappeared after restarting the IPython shell.

Notability answered 12/4 at 22:26 Comment(0)
P
-1

try adding null and blank field

members = models.ManyToManyField(user, blank=True, null=True)
Palaeontology answered 21/12, 2020 at 11:22 Comment(0)
C
-2

There is propably duplicate items in the datebase.

You can check by listing all items in model using:

YourModel.objects.values_list('id', 'name')

To avoid it make sure to set unique=True.

name = models.CharField(max_length=200, unique=True)
Chronaxie answered 25/2, 2017 at 7:51 Comment(1)
How would a duplicate folder name cause the OP's issue?Pashto

© 2022 - 2024 — McMap. All rights reserved.