Django admin site not displaying ManyToManyField relationship

I'm working on what I think is a pretty standard django site, but am having trouble getting my admin section to display the proper fields.

Here's my

class Tech(models.Model):
    name = models.CharField(max_length = 30)

class Project(models.Model):
    title = models.CharField(max_length = 50)
    techs = models.ManyToManyField(Tech)

In other words, a Project can have different Tech objects and different tech objects can belong to different Projects (Project X was created with Python and Django, Project Y was C# and SQL Server)

However, the admin site doesn't display any UI for the Tech objects. Here's my

class TechInline(admin.TabularInline):
    model = Tech
    extra = 5

class ProjectAdmin(admin.ModelAdmin):
    fields = ['title']
    inlines = []
    list_display = ('title'), ProjectAdmin)

I've tried adding the TechInline class to the inlines list, but that causes a

<class 'home.projects.models.Tech'> has no ForeignKey to <class 'home.projects.models.Project'>

Error. Also tried adding techs to the fields list, but that gives a

no such table: projects_project_techs

Error. I verified, and there is no projects_project_techs table, but there is a projects_tech one. Did something perhaps get screwed up in my syncdb?

I am using Sqlite as my database if that helps.

Asked by: John408 | Posted: 05-10-2021

Answer 1

I've tried adding the TechInline class to the inlines list, but that causes a

'TechInLine' not defined

Is that a straight copy-paste? It looks like you just made a typo -- try TechInline instead of TechInLine.

If your syncdb didn't create the proper table, you can do it manually. Execute this command:

python sqlreset <myapp>

And look for the definition for the projects_project_techs table. Copy and paste it into the client for your database.

Answered by: Byron786 | Posted: 06-11-2021

Answer 2

Assuming your app is called "projects", the default name for your techs table will be projects_tech and the projects table will be projects_project.

The many-to-many table should be something like projects_project_techs

Answered by: John491 | Posted: 06-11-2021

Answer 3

@John Millikin - Thanks for the sqlreset tip, that put me on the right path. The sqlreset generated code that showed me that the projects_project_techs was never actually created. I ended up just deleting my deb.db database and regenerating it. techs then showed up as it should.

And just as a sidenote, I had to do an to be able to create new instances of the class from the Project page too.

I'll probably post another question to see if there is a better way to implement model changes (since I'm pretty sure that is what caused my problem) without wiping the database.

Answered by: Blake239 | Posted: 06-11-2021

Similar questions

python - Add ManyToManyField relationship on save, and create objects if they not exist

I want to 'attach' a manytomany relationship on form submit. The example is the classic blog tags-post relation: a post can have multiple tags related In django-admin it works, but i can't figure how to do in views... my code: def add_post(request): if request.method == 'POST': form = PostForm(data=request.POST) if form.is_valid(): model_instance = for...

python - ManyToManyField relationship retrieving no data

I've one structure like this: 1. Author 2. Book 3. AuthorType 4. AuthorBookType A book can have more than one author, and it can have functions inside the book like: "Author", "Co-Author", "Part", "Helper", Etc: class Book(models.Model): title=models.CharField(max_length=100) class Author(models.Model): name=models.CharField(max_length=100) books=models.ManyToManyField(Book, throu...

python - Django ManyToManyField reverse relationship issues

I have 3 models, Industry has a ManyToManyField to Client and Contact has a ForeignKey to Client. When I go to the django admin, Contact and Industry both display the correct widgets and allow for choosing the relationship and they seem to work. But if I try to access a Client I created, I get this error:

python - splitting a ManyToManyField over multiple form fields in a ModelForm

So I have a model with a ManyToManyField called tournaments. I have a ModelForm with two tournament fields: pay_tourns = forms.ModelMultipleChoiceField( queryset=Tourn.objects.all().active().pay_tourns(), widget=forms.CheckboxSelectMultiple()) rep_tourns = forms.ModelMultipleChoiceField( queryset=Tourn.objects.all().active().rep_tourns(...

python - Django: queryset filter for *all* values from a ManyToManyField

Hi (sorry for my bad english :p) Imagine these models : class Fruit(models.Model): # ... class Basket(models.Model): fruits = models.ManyToManyField(Fruit) Now I would like to retrieve Basket instances related to all fruits. The problem is that the code bellow returns Basket instances related to any fruits : baskets = Basket.objects.filter...

python - Django : save a new value in a ManyToManyField

I gave details on my code : I don't know why my table is empty (it seems that it was empty out after calling save_model, but I'm not sure). class PostAdmin(admin.ModelAdmin): def save_model(self, request, post, form, change): # Authors must be saved after saving post print form.cleaned_data['authors'] # [] print request.user # pg authors ...

python - Django Admin: Ordering of ForeignKey and ManyToManyField relations referencing User

I have an application that makes use of Django's UserProfile to extend the built-in Django User model. Looks a bit like: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) # Local Stuff image_url_s = models.CharField(max_length=128, blank=True) image_url_m = models.CharField(max_length=128, blank=True) # Admin class Admin: pass

python - adding the same object twice to a ManyToManyField

I have two django model classes: class A(models.Model): name = models.CharField(max_length = 128) #irrelevant class B(models.Model): a = models.ManyToManyField(A) name = models.CharField(max_length = 128) #irrelevant What I want to do is the following: a1 = A() a2 = A() b = B() b.a.add(a1) b.a.add(a1) #I want to have a1 twice b.a.add(a2) assert le...

python - Django: apply "same parent" constraint to ManyToManyField mapping to self

I have a model where tasks are pieces of work that each may depend on some number of other tasks to complete before it can start. Tasks are grouped into jobs, and I want to disallow dependencies between jobs. This is the relevant subset of my model: class Job(models.Model): name = models.CharField(max_length=60, unique=True) class Task(models.Model): job = models.ForeignKey(Job) prerequisites =...

python - How can I sort by the id of a ManyToManyField in Django?

I've got a ManyToManyField in a user object and it's used to map the users that user is following. I'm trying to show a subset list of who they have most recently followed. Is there a trick in .order_by() that will allow me to sort by the id of the ManyToManyField? The data is there, right? # (people the user is following) following = models.ManyToManyField(User, related_name="following", blank=True) theus...

python - Django: Is there a way to have the "through" model in a ManyToManyField in a different app to the model containing the ManyToManyField?

Lets say I have two django apps: competitions - which will handle competition data entries - which will handle functionality relating to entering competitors into competitions In the competitions app I have a model which represents a section of a competition: class Division(models.Model): competition = models.ForeignKey(Competition)...

python - Django ManyToManyField Creation Problems

I currently have these models: class Category(models.Model): name = models.CharField(max_length=200) parent = models.ForeignKey('self', blank=True, null=True, related_name='child') description = models.TextField(blank=True,null=True) class Item(models.Model): name = models.CharField(max_length=500) ... tag = models.ManyToManyField(Category, null=True, blank=True, related_name='tag_i...

python - Is a reason I can't add a ManyToManyField?

So I'm building a Django application, and these are a few models I have: class MagicType(models.Model): name = models.CharField(max_length=155) parent = models.ForeignKey('self', null=True, blank=True) class Spell(models.Model): name = models.CharField(max_length=250, db_index=True) magic_words = models.CharField(max_length=250, db_index=True) magic_types = models.ManyToManyField(Ma...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python