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?

citos88
  • 141
  • 6
  • 2
    `{% for user in current_folder.company.members.all %} {% if user %} ....` – Avinash Raj Mar 15 '16 at 09:22
  • For extra context, I'm having the same issue - but only on travis-CI when running tests. –  Aug 29 '16 at 21:47
  • And for completion, this is because I was running v1.8 of Django, not 1.8.x –  Aug 29 '16 at 22:33
  • 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. – Brandon Dewey Nov 23 '16 at 16:49
  • 1
    Same here working with django on a jupyter notebook – Daniel Aron Goldenberg Aug 04 '19 at 02:08
  • 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. – michjnich Jul 29 '20 at 09:03
  • 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? – auvipy Feb 22 '21 at 11:17

5 Answers5

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.

auvipy
  • 769
  • 10
  • 22
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

Shedrack
  • 656
  • 7
  • 22
-1

In ManyToManyFields you must add the related_name parameter if you want to access the related objects. So for this case, it should be like this:

class Company(models.Model):
    name = models.CharField(max_length=200)
    members = models.ManyToManyField(User, related_name='members')
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)
  • Nope, he's accessing it correctly. The field is by default accessible as User.company_set, with your code the User model would have a field members, which would point to Company and that's pretty wrong – Jura Brazdil Dec 14 '20 at 11:51
-1

try adding null and blank field

members = models.ManyToManyField(user, blank=True, null=True)
vineet
  • 850
  • 7
  • 9
-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)
King110
  • 122
  • 8