0

I have Profile Model that have instance model user.. I am trying to create group using form. that group model have Profile model as instance, so how do I select authenticated user automatic to create group, I getting confused...

This is my Group Model

class Groups(models.Model):
profile = models.ForeignKey(Profile, related_name='my_groups', on_delete=models.CASCADE)
groups_name = models.CharField(max_length=150)
slug = models.SlugField(max_length=150, unique=True)
cover_pic = models.ImageField(upload_to='images/groups/', null=True, blank=True)
type_group = models.CharField(max_length=150)
created_date = models.DateTimeField(auto_now_add=True)
about_group = models.TextField(max_length=600)
members = models.ManyToManyField(Profile,through="GroupMember")

def __str__(self):
    return self.groups_name

This is my profile Model

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
slug = models.SlugField(max_length=100, unique=True)
profile_pic = models.ImageField(upload_to='images/user/profile/', null=True, blank=True)
cover_pic = models.ImageField(upload_to='images/user/profile/', null=True, blank=True)
user_bio = models.TextField(null=True,blank=True, max_length=255)
designation = models.CharField(blank=True,null=True, max_length=255)
education = models.CharField(blank=True, null=True, max_length=255)
marital_status = models.CharField(blank=True, null=True, max_length=60)
hobbies = models.CharField(blank=True, null=True, max_length=500)
location = models.CharField(blank=True, null=True, max_length=500)
mobile = models.IntegerField(blank=True, null=True)

def __str__(self):
    return str(self.user)

This is form for create group

class GroupCreateForm(forms.ModelForm):
class Meta:
    model = Groups
    fields = ('cover_pic', 'profile', 'groups_name', 'type_group', 'about_group')

profile = forms.CharField(widget=forms.TextInput(attrs={
    'class': 'form-control input-group-text',
    'value':'',
    'id':'somesh',
    'type': 'hidden',


}))

This is create group html file

this is html page

this is error..

somesh
  • 5
  • 2
  • Please include *code*, not *images of code*: see this question named [*Why not upload images of code errors when asking a question*](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). – Willem Van Onsem Aug 11 '21 at 18:38
  • 1
    Also add the relevant (class-based) view. – Willem Van Onsem Aug 11 '21 at 18:38

1 Answers1

0

You have set a CharField for profile in your form. When you send data to this form, it tries to make a Group record with a FK to a CharField for example "1" and this gives you error because you should pass Profile object to Group.

Depends on what exactly you want, there are some options.

You can use ModelChoiceField. Read about it in https://docs.djangoproject.com/en/3.2/ref/forms/fields/#django.forms.ModelChoiceField

Or you can use Inline FormSet and read about it in https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets

And an example for Inline FormSet in Creating a model and related models with Inline formsets

Amin
  • 2,605
  • 2
  • 7
  • 15