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

  • Home
  • SEARCH
  • 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 7416435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:29:45+00:00 2026-05-29T07:29:45+00:00

In models.py : from django.db import models from django.utils.translation import ugettext as _ #

  • 0

In models.py:

from django.db import models
from django.utils.translation import ugettext as _

# Create your models here.
class Category(models.Model):
    name            = models.CharField(_(u"Name"), max_length=250)
    products        = models.ManyToManyField("Product", verbose_name=_(u"Products"), \
                      blank=True, null=True, related_name="+")
        
class Product(models.Model):
    name            = models.CharField(_(u"Name"), max_length=250)    
    category        = models.ManyToManyField("Category", verbose_name=_(u"Category"), \
                      blank=True, null=True, related_name="+")

In admin page:

m2m relationships

The question:
How can the relations between products and category m2m fields in models.py be set so that in admin-page, as it can be seen at the picture, b2(the product) was marked as it belongs to a2 (the category).
Any advices for implementation of [products, categories] are welcomed, thank you.

P.S.
I’m newbie in Django. Sorry for my English.

  • 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-05-29T07:29:46+00:00Added an answer on May 29, 2026 at 7:29 am

    The problem is that you have two ManyToMany fields. As you’ve noted, when the relationship is set in one of them, it isn’t in the other.

    The solution is simple: drop one of the fields. You only need a ManyToManyField on one side of the relationship. Django gives you access to the other side automatically. So, if you kept the categories field on the Product model, you can do my_product.categories.all() to get the categories a product is associated with; and my_category.product_set.all() to get the products that belong to a category.

    You’ll also need to drop the related_name="+" attribute: you’ve presumably put that in because you were getting conflicts, which should have been a clue.

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

Sidebar

Related Questions

Given: from django.db import models class Food(models.Model): Food, by name. name = models.CharField(max_length=25) class
Given a class: from django.db import models class Person(models.Model): name = models.CharField(max_length=20) Is it
from django.db import models class Post(models.Model): title = models.CharField(max_length=100) content = models.CharField(max_length=1000) created =
I have a Model and ModelForm like: from django.utils.translation import ugettext_lazy as _ class
Given a Django model likeso: from django.db import models class MyModel(models.Model): textfield = models.TextField()
I have a model project_phase : from django.db import models from django.utils import simplejson
I inherited form the django user model like so: from django.db import models from
When using from django.utils import simplejson on objects of types that derive from db.Model
from django import forms class UserForm(forms.ModelForm): first_name = forms.TextField(label=_(u'First name'), required=False) last_name = forms.TextField(label=_(u'Last
I have the following models: http://slexy.org/view/s20T8yOiKZ from mxutils.cms_services import generate_secid from django.db import models

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.