0

I'm new to Django and I have a question working with ManyToManyFields in Django with Admin.

So I have two model classes.

class Tag(models.Model):
    tag = models.CharField(max_length=100)
    def __str__(self):
        return self.tag
    class Meta:
        ordering = ['tag']
        verbose_name = "tag"
        verbose_name_plural = "tags"

class MyiPhoneApp(models.Model):
    tags = models.ManyToManyField(Tag)

    name = models.CharField(max_length=200)
    bundleID = models.CharField(max_length=200, primary_key=True)
    def __str__(self):
        return self.name
    class Meta:
        ordering = ['name']
        verbose_name = "iPhone App"
        verbose_name_plural = "iPhone Apps"

So when I register this classes to Admin I can add some iPhone Apps. But after adding one App with 2 tags and saving, there is happing something strange when I'm adding a second app or more.

every App has every tag attribute.

I thought that a many to many field is the right relationship to represent this.

One App has many Tags and One Tag has Many Apps

But now every App has every available tag from db assigned and this behaviour is wrong :/

Is my relationship wrong or have I forget something to add to my (model)code?

Miralem Cebic
  • 1,276
  • 1
  • 15
  • 28
  • The code you posted isnt causing this behavior. Can you post any other related code? Admin.py perhaps, any forms, and any overridden save functions – skzryzg May 10 '14 at 12:12
  • Sorry but I have to get this possibility out of the way, are you checking tags related to apps via a shell or via the admin ? If it is via the admin, are you certain the tags are selected and not just displayed in the widget ? – Ambroise May 10 '14 at 12:41

2 Answers2

0

the project name is "helloworld_2", the app name, created with manage.py start app, is "MyApps"

in settings.py, "MyApps" is in INSTALLED_APPS

in models.py from MyApps the code is in my first post above.

in admin.py in MyApps there are four lines of code:

from django.contrib import admin
from MyApps.models import Tag, MyiPhoneApp

# Register your models here.

admin.site.register(Tag)
admin.site.register(MyiPhoneApp)

So thats it. After syncdb and run server I go to localhost:8000/admin

My first App that I creating is: name: "BlahBlah App" bundle_id: "com.mir.msg.blah"

with the tags: "communication" and "messenger"

after saving, I want to create a second app. And there is it, the 2 tags "communication" and "messenger" are automatically in the "Tags" field.

First saved App

New App, without any of my input, there are the two tags displayed

New App, without any of my input, there are the two tags displayed

Miralem Cebic
  • 1,276
  • 1
  • 15
  • 28
  • This should have been added to the question, not an answer. But the screen shot you show simply has this two tags as available for adding to that app. You can see that neither of them is selected, do neither is actually associated with the app. – Daniel Roseman May 10 '14 at 22:15
0

Actually, you have no options selected. that is just the list where you can select the tags . As you can read, you can select one or more options using ctrl or cmd if you're on the apple side of the moon.

valleymanbs
  • 487
  • 3
  • 14