У меня есть модель Meeting
, у которой meeting_hour
получает значение от модели MeetingHour
. Но я получаю сообщение об ошибке после отправки формы:
Cannot assign "datetime.time(14, 0)": "Meeting.meeting_hour" must be a "MeetingHour" instance.
Я знаю, потому что я не использую MeetingHour
в качестве внешнего ключа правильно, но не не знаю, как я могу это исправить. Вот мои коды:
models.py
class MeetingHour(models.Model):
hour = models.TimeField()
def __str__(self):
return self.hour
class Meeting(models.Model):
participant_name = models.CharField(max_length=50)
participant_email = models.EmailField(max_length=50)
meeting_date = models.DateField()
meeting_hour = models.ForeignKey(MeetingHour, on_delete = models.CASCADE)
is_scheduled = models.BooleanField(default=False)
def __str__(self):
return self.participant_name
views.py
def index(request):
context = {
'schedules': Meeting.objects.all()
}
if request.method == "POST":
participant_name = request.POST.get('name')
participant_email = request.POST.get('email')
meeting_date = str(request.POST.get('date'))
meeting_hour = str(request.POST.get('hour'))
converted_meeting_date = datetime.strptime(request.POST.get('date'), "%Y-%m-%d").date() if meeting_date else None
converted_meeting_hour = datetime.strptime(request.POST.get('hour'), "%H:%M").time() if meeting_hour else None
subject = 'Meeting'
message = f'Hi, {participant_name} ! \nYour meeting date: {meeting_date}, hour: {meeting_hour}'
from_email = settings.SERVER_EMAIL
recipient_list = [participant_email]
send_mail(subject, message, from_email, recipient_list)
if request.POST.get('email'):
Meeting.objects.create(
participant_name = request.POST.get('name'),
participant_email = request.POST.get('email'),
meeting_date = converted_meeting_date,
meeting_hour = converted_meeting_hour,
is_scheduled = True
)
return render(request, 'index.html', context)