How can i pass the current instance of a model(Movie) to Seat model as a foreignkey?
Here’s a sample code:
class Movie(models.Model):
title = models.CharField(max_length=150, unique=True)
def _create_seats(self):
seat = Seat.objects.update_or_create(
movie="Here is my problem",
)
def save(self, *args, **kwargs):
self._create_seats()
super(Movie, self).save(*args, **kwargs)
class Seat(models.Model):
movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
I am trying to generate a data for a seats when a movie is created, the update_or_create; just to verify if the seat was already created else, update it when the customer paid the seat.
So you’re going to have a view that results in creating the Movie - at that point, create the related seats. Likewise, when a user pays for the seat, that occurs in a view - that’s where you want to update the seat. You have all the information you need at that point.
(When you’re posting code, enclose it between lines consisting of only three backtick ` characters - that keeps the formatting cleaner and makes it easier to read. So you’ll have a line of ```, your code, and then another line of ```. Make sure you use the backtick - ` and not the apostrophe - '. If you would, please edit your prior reply, thanks.)
Ok, so in the context of your real code, what’s the issue you’re trying to address?
Basically the flow, is i have the Movie class and CinemaSeat class, these two classes can only be access in admin panel. So every-time i create a movie for example(“Marvel”) then i save it; this Movie class will call the CinemaSeat class to generate/create a seat for a theater.
Are you asking how you set the movie attribute? If so, it looks like you’re already setting it.
If not, what is the question you’re trying to get answered?
Yes that is exactly my current problem, i cannot set a value to a movie, since in CinemaSeat, the movie field is set as a foreignkey. That one throws an error. It says "Cannot assign "'Marvel'": "CinemaSeat.movie" must be a "Movie" instance."
I think because the movie is not existed yet. So basically, what i am trying to do is not possible? Because i have to create the movie first then override the save() to generate a seats, but the CinemaSeat requires a movie which is a foreignkey