citos88
citos88

Reputation: 141

FieldDoesNotExist: ManyToManyField has no field named None

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?

Upvotes: 13

Views: 2601

Answers (5)

tehfink
tehfink

Reputation: 477

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.

Upvotes: 0

Shedrack
Shedrack

Reputation: 743

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

Upvotes: 0

auvipy
auvipy

Reputation: 1208

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.

Upvotes: 1

vineet
vineet

Reputation: 964

try adding null and blank field

members = models.ManyToManyField(user, blank=True, null=True)

Upvotes: -1

King110
King110

Reputation: 122

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)

Upvotes: -2

Related Questions