# Many-to-One model not saving in admin

**URL:** <https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331>\
**Category:** The Admin\
**Created:** [November 14, 2023, 2:31pm UTC](https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331 "2023-11-14T14:31:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tallmtt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tallmtt/32/17087_2.png) [@tallmtt](https://forum.djangoproject.com/u/tallmtt)\
**Post date:** [November 14, 2023, 2:31pm UTC](https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331/1 "2023-11-14T14:31:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 14, 2023, 2:39pm UTC](https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331/2 "2023-11-14T14:39:22Z")

</div>

> [@tallmtt](#):
>
> I have read about Many-to-Many not saving in Admin

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

> [@tallmtt](#):
>
> Is this is the same issue?

Is _what_ the same issue?

---

<div class="post-metadata">

**Author:** ![tallmtt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tallmtt/32/17087_2.png) [@tallmtt](https://forum.djangoproject.com/u/tallmtt)\
**Post date:** [November 14, 2023, 2:56pm UTC](https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331/3 "2023-11-14T14:56:58Z")

</div>

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

> [@tallmtt](#):
>
> I have read about Many-to-Many not saving in Admin

I am referring to these types of pages:

- [python - ManyToMany field not saved when using Django admin - Stack Overflow](https://stackoverflow.com/questions/6200233/manytomany-field-not-saved-when-using-django-admin)
- [[Answered]-ManyToMany field not saved when using Django admin-django](https://www.appsloveworld.com/django/100/15/manytomany-field-not-saved-when-using-django-admin)  
(I had more links but as a new user, I can only put 2 links in a post)

> [@tallmtt](#):
>
> Is this is the same issue?

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 14, 2023, 3:02pm UTC](https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331/4 "2023-11-14T15:02:54Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![tallmtt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tallmtt/32/17087_2.png) [@tallmtt](https://forum.djangoproject.com/u/tallmtt)\
**Post date:** [November 14, 2023, 4:38pm UTC](https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331/5 "2023-11-14T16:38:28Z")

</div>

Here are the two models I’m referring to:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 14, 2023, 4:49pm UTC](https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331/6 "2023-11-14T16:49:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tallmtt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tallmtt/32/17087_2.png) [@tallmtt](https://forum.djangoproject.com/u/tallmtt)\
**Post date:** [November 14, 2023, 4:50pm UTC](https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331/7 "2023-11-14T16:50:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tallmtt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tallmtt/32/17087_2.png) [@tallmtt](https://forum.djangoproject.com/u/tallmtt)\
**Post date:** [November 14, 2023, 4:54pm UTC](https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331/8 "2023-11-14T16:54:38Z")

</div>

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:

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [November 14, 2023, 5:29pm UTC](https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331/9 "2023-11-14T17:29:09Z")

</div>

You have problems with your model:

> [@tallmtt](#):
>
> ```auto
> 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
> 
> ```

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.)

---

<div class="post-metadata">

**Author:** ![tallmtt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/tallmtt/32/17087_2.png) [@tallmtt](https://forum.djangoproject.com/u/tallmtt)\
**Post date:** [November 14, 2023, 6:23pm UTC](https://forum.djangoproject.com/t/many-to-one-model-not-saving-in-admin/25331/10 "2023-11-14T18:23:24Z")

</div>

> [@KenWhitesell](#):
>
> Notice that you are _not_ calling `super` if `self.slug` has a value.

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