1

I have a field in my Django model defined as:

def get_field(**kwargs):
    kwargs.setdefault('related_name', '+')
    return models.ManyToManyField('app.MyOtherModel', **kwargs)

class MyModel(models.Model):
    MY_FIELD = 'A label for my field'
    my_field = get_field() # wrote function to DRY creation of fields

But whenever I try to add something to the field, it doesn't work:

foo = MyOtherModel.objects.create(...)
m = MyModel.objects.get(...)
m.my_field.add(foo)
m.my_field.all() # returns []

Doing some digging into Django source code, the ManyRelatedManager.add() function (in django/db/models/fields/related.py) works correctly, with self.through.objects.all() returning the right object at the end of the function, but self.all() doesn't, leading me to think the problem is in get_queryset():

# printing self.through.objects.all() @ related.py:978
[<MyModel_myothermodel: MyModel_myothermodel object>]

# printing self.all() @ related.py:978
[]

Is the problem in the fact that I have both MY_FIELD and my_field defined in MyModel? Or maybe in how I generate the ManyToManyField? Or is there some other issue?

UPDATE: The issue is in related.py in the get_queryset function. self.core_filters is set to {'+__year': 2015}, however, when I query qs.values('+__year'), I get {'+__year': None}. (year is another field in the model)

4
  • 1
    You shouldn't generally mess about with the way fields are defined. There's a whole lot of metaclass stuff that makes the declarative syntax work, and I doubt that it would work with your separate function. Oct 5, 2015 at 20:31
  • Why do you have the weird setup for your my_field? Wouldn't a normal m2m field does your work?
    – Shang Wang
    Oct 5, 2015 at 20:32
  • Roseman- the migration file worked, though. You still think it's not the best? Would it work better if I maybe did MY_FIELD_LABEL instead? // Wang- the weird setup is because I have around 50 ManyToMany fields, and I'd like to be able to access the names of the fields from the class level. Oct 5, 2015 at 20:54
  • I'm not sure I've ever been so curious about exactly what someone's doing and why as this question... You may not want to reveal more about it, but I'm definitely curious if you don't mind divulging more..? Maybe there's a better way entirely..?
    – shark3y
    Aug 23, 2016 at 19:28

1 Answer 1

1

I think I found my answer in old Django documentation: https://docs.djangoproject.com/en/1.4/ref/models/fields/#django.db.models.ManyToManyField.related_name

I need to specify unique related_name values, even if I use +. So I need to do related_name='foo+' and related_name='bar+'

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.