Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8385371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:39:10+00:00 2026-06-09T17:39:10+00:00

I have a model like this: class Book(models.Model): authors = models.ManyToManyField(User) # User is

  • 0

I have a model like this:

class Book(models.Model):
     authors = models.ManyToManyField(User) # User is a separate model
     readers = models.ManyToManyField(User)
     # ... rest of code

I’m trying to create a function outside my views which takes three inputs: a Book, a string and a User. The User is added to the Book’s ManyRelatedManager whose corresponding field name matches the string.

For example, if the function were called foo:

def foo(book, field_name, user):
    f = #... this is where I need help, should be a ManyRelatedManager that belongs to 'book'
    f.add(user)
    book.save()

Calling foo(Book.objects.get(id=1), 'authors', User.objects.get(id=1)) would result in the User with id 1 being added to the field ‘authors’ in the Book with id 1. Similarly, calling foo(Book.objects.get(id=1), 'readers', User.objects.get(id=1)) would result in the User with id 1 being added to the field ‘readers’ in the Book with id 1.

I tried using Book._meta.get_field() but this returned the ManyToManyField itself, not a ManyRelatedManager. Is there some sort of built-in way to get the ManyRelatedManager whose field name matches a string?

Sorry if that description is confusing, my problem is hard to put into words. If anyone can understand my problem, your help is appreciated. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:39:12+00:00Added an answer on June 9, 2026 at 5:39 pm

    getattr() can get an attribute from an object from a string passed to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a model like this: class Book(models.Model): user = models.ForeignKey(User) book_isbn =
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
I have a model.py like this: class EventTypeCategory(models.Model): name = models.CharField(max_length=50, verbose_name=Name) user =
I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
I currently have this chunk of model code: class Book(models.Model): title = models.CharField(max_length=100) num_pages
Given a model structure like this: class Book(models.Model): user = models.ForeignKey(User) class Readingdate(models.Model): book
I have some models that look like this: class UserProfile(models.Model): user = models.OneToOneField(User) favorite_books
i've a problem with my tabularinline field. I have model like this class Product(models.Model):
I have a simple model like this: class User < ActiveRecord::Base serialize :preferences end
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.