1

I have checked all the solutions related to my question but no one worked, i have an event table in which i am assigning the id of user. Event Model is

class Event(models.Model):
    user_id=models.ForeignKey(User, on_delete=models.CASCADE)
    event_auth_id=models.CharField(null=True, max_length=225)
    event_title=models.CharField(max_length=225)
    ticket_title=models.CharField(max_length=225)
    category=models.CharField(max_length=50)
    event_summary=models.TextField()
    event_information=models.TextField()
    restriction=models.CharField(max_length=50, default='No Restriction')
    artist_image=models.CharField(null=True, max_length=50)
    event_poster=models.CharField(null=True, max_length=50)
    notification_email=models.CharField(null=True, max_length=50)
    notification_frequency=models.CharField(null=True, max_length=15)
    name_on_ticket=models.CharField(max_length=225)
    event_tnc=models.TextField()
    current_step=models.IntegerField(null=True)
    event_status=models.BooleanField(default=True)
    created=models.DateTimeField(auto_now=True)
    modified=models.DateTimeField(auto_now_add=True)

I am assigning the logged in user id from view

def create_new_event(request, steps):
if request.method == 'POST':
    if(steps=="step_1"):
        stepFirstForm = CreateEventStepFirstForm(request.POST)
        if stepFirstForm.is_valid():
            eventStepFirst = Event(
                user_id = request.user.id,
                event_auth_id = uuid4(),
                event_title = request.POST['event_title'],
                ticket_title = request.POST['ticket_title'],
                category = request.POST['categories'],
                event_summary = request.POST['event_summary'],
                event_information = request.POST['event_information'],
                restriction = request.POST['restrictions'],
                notification_email = request.POST['notification_email'],
                notification_frequency = request.POST['email_frequency']
            )

But its giving me error

Cannot assign "42": "Event.user_id" must be a "User" instance.    
Pankaj
  • 229
  • 5
  • 17

2 Answers2

2

The problem is in this code:

    def create_new_event(request, steps):
    if request.method == 'POST':
        if(steps=="step_1"):
            stepFirstForm = CreateEventStepFirstForm(request.POST)
            if stepFirstForm.is_valid():
                eventStepFirst = Event(
                    user_id = request.user.id,
                    event_auth_id = uuid4(),
                    event_title = request.POST['event_title'],
                    ticket_title = request.POST['ticket_title'],
                    category = request.POST['categories'],
                    event_summary = request.POST['event_summary'],
                    event_information = request.POST['event_information'],
                    restriction = request.POST['restrictions'],
                    notification_email = request.POST['notification_email'],
                    notification_frequency = request.POST['email_frequency']
                )

In the place of "user_id = request.user.id" You should use "user_id = request.user" or "user_id = request.user.username" because in the field

  user_id=models.ForeignKey(User, on_delete=models.CASCADE)

of Event model,you assigned user_id as a foreign key of User model,So user_id field is expecting a User instance,not request.user.id

Thanks.

Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
1

The ForeignKey field is expecting an object of type User, not the user ID. Try changing the assignment user_id = request.user.id to user_id = request.user. It might also make sense to rename the field to "user" to avoid confusion in the future.

Mike Pelley
  • 2,939
  • 22
  • 23