Many-to-One model not saving in admin

I have read about Many-to-Many not saving in Admin and I wonder if

  1. Is this is the same issue?
  2. What is the best practice to address it? Is it to make a special save function, or just make a form outside of Admin to save (which I haven’t tried yet)?

I have a model that extends the User model (team member) and another model that I want users to relate to, but that could have multiple users (team).

In the Admin area, the Team can be selected within the Team member model, but after saving and reopening the Team member, it is not connected.

Where? What context?
When handled correctly, many-to-many relationships work perfectly well in the admin.

Is what the same issue?

Thank you for your quick response! I was trying to be efficient and concise, and probably was too vague in my efforts.

I am referring to these types of pages:

To clarify:

I have a many-to-one relationship defined between 2 models. When I associate data from the many side model to the one sided model and click save in the Admin area, the database does not reflect this save.

How should I investigate this?

I was asking if it is the same reason that some situations many-to-many doesn’t save in admin, but that is probably unnecessary information. I really only care about getting my many-to-one to save right now.

Thanks again, I do appreciate all the insights those on here can share

That first post is dangerously out-of-date, and from SO, so I put no value in it at all. I can’t tell where that other post comes from.

If you’re having a specific issue with your implementation, I’d suggest you post your models and your model admin classes here for us to be able to assist you.

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code then another line of ```. (Do this for each file.)

Here are the two models I’m referring to:

class Team(models.Model):
    team_name = models.CharField(max_length=50)
    team_city = models.CharField(max_length=50)
    slug = models.SlugField(blank=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.team_name)
            super(League, self).save(*args, **kwargs)

    def __str__(self):
        return self.team_name

class Teammember(models.Model): # Using django.contrib.auth.models import User
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    team_affiliation = models.ForeignKey(Team, on_delete=models.CASCADE, null=True) # One-to-Many
    slug = models.SlugField(blank=True)
    fun_name = models.CharField(max_length=50, blank=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.fun_name)
            super(Teammember, self).save(*args, **kwargs)

    def __str__(self):
        return self.fun_name

Your calls to super make references to classes that your models don’t seem to be related to. What are those classes and how do they relate to these? (And why are you trying to call super on them?)

Also, which class are you having the issue with? What is its ModelAdmin class?

You were right - I had edited it to try to not confuse the issue. I have now made it consistent

I have data in Team (say Team 1) and a Teammember who is also a user.

When I try to assign the Teammember in the Teammember model in Admin to a Team (Team 1) in Admin, I click the save button, it appears to save, but opening up Teammember again, and it is not saved.

I am not using ModelAdmin.

My admin.py:

from django.contrib import admin

from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User

# Register your models here.

from .models import Teammember, Team

admin.site.register(Teammember)
admin.site.register(Team)

You have problems with your model:

Notice that you are not calling super if self.slug has a value. You need to call super for the models regardless of the status of the slug field. (You need to do this with both models.)

You are brilliant! I commented out those save functions and it works as expected. Thank you for your help!