# Unable to save ManyToManyField in admin panel as well as using ModelForm

**URL:** <https://forum.djangoproject.com/t/unable-to-save-manytomanyfield-in-admin-panel-as-well-as-using-modelform/23401>\
**Category:** Using the ORM\
**Created:** [August 26, 2023, 10:51am UTC](https://forum.djangoproject.com/t/unable-to-save-manytomanyfield-in-admin-panel-as-well-as-using-modelform/23401 "2023-08-26T10:51:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerkd](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rogerkd/32/15608_2.png) [@rogerkd](https://forum.djangoproject.com/u/rogerkd)\
**Post date:** [August 26, 2023, 10:51am UTC](https://forum.djangoproject.com/t/unable-to-save-manytomanyfield-in-admin-panel-as-well-as-using-modelform/23401/1 "2023-08-26T10:51:55Z")

</div>

**models.py**

class Profile(models.Model):

```
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_image = models.ImageField(upload_to='profile_images/', blank=True, null=True, verbose_name='Profile Picture')
bio = models.TextField(blank=True, null=True)
following = models.ManyToManyField(
    'self',
    related_name="followers",
    symmetrical=False,
    blank=True
)

def follow(self, user):
    self.following.add(user)

def unfollow(self, user):
    self.following.remove(user)

def is_following(self, user):
    return self.following.filter(pk=user.pk).exists()

def __str__ (self):
    return "{}".format(self.user.username)

# def save(self, *args, **kwargs):
 
# super(Profile, self).save(*args, **kwargs)

```

class Blog(models.Model):

```
author = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
title = models.CharField(max_length=200)
description = models.TextField(blank=True, null=True)
blog_image = models.ImageField(upload_to='blog_images/', blank=True, null=True, verbose_name='Blog Picture')
date = models.DateTimeField(auto_now_add=True)

def __str__ (self):
    return "{} {} {}".format(self.author, self.title, self.date)

class Meta:
    ordering = ["date"]

```

**forms.py**

class BlogForm(forms.ModelForm):

```
class Meta:
    model = Blog
    fields = ('title', 'description', 'blog_image')

```

class ProfileForm(forms.ModelForm):

```
class Meta:
    model = Profile
    fields = ('profile_image', 'bio', 'following')

```

**views.py**

class CreateProfile(LoginRequiredMixin, CreateView):

```
model = Profile
form_class = ProfileForm
context_object_name = 'create_profile'
template_name = 'blog/create_profile.html'
success_url = reverse_lazy('home')

def form_valid (self, form):
    form = ProfileForm(self.request.POST, self.request.FILES)
    instance = form.save(commit=False)
    instance.save()
    form.save_m2m() # Save the ManyToManyField 'following'

    return super(CreateProfile, self).form_valid(form)

```

---

<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:** [August 27, 2023, 12:27am UTC](https://forum.djangoproject.com/t/unable-to-save-manytomanyfield-in-admin-panel-as-well-as-using-modelform/23401/2 "2023-08-27T00:27:09Z")

</div>

You’re going to need to provide more detail and be more descriptive about the problem you’re having.

These details need to include what you’re seeing that you don’t expect to see - or what you’re not seeing that you’re expecting to see.

Include all error messages that you may be getting in the console where you’re running runserver.

Post your ModelAdmin class that you’re saying is not working.

Side note, Don’t do this:

> [@rogerkd](#):
>
> ```auto
> form = ProfileForm(self.request.POST, self.request.FILES)
> 
> ```

The form has already been instantiated from the POST data earlier in the view processing, along with having been validated.

Also, since you’re saving the form here, you should _not_ be calling `super` in `form_valid`. After you’ve done your work here, return your HttpResponseRedirect to your `success_url`.

If you’re not familiar with the [Classy Class-Based Views](https://ccbv.co.uk/) site, I suggest you take some time reading it to help you understand what each of the functions being performed do. (Personally, I find that site to be more useful than the official docs when it comes to understanding the CBVs.)
