0

Below are my Models

class Seminar(models.Model):
seminarID = models.AutoField(primary_key=True)
presenter_name = models.CharField(max_length=200)
location_name = models.TextField()
seminar_DT = models.DateTimeField(default=datetime.datetime.now)
capacity = models.IntegerField(default=50)

class Registration(models.Model):
registration_ID = models.AutoField(primary_key=True)
seminar=models.ForeignKey(Seminar,to_field='seminarID',on_delete=models.CASCADE)      
attendee_name = models.CharField(max_length=200,null=True)
email_address = models.EmailField()
email_sent = models.BooleanField(default=False)

Here is the ModelForm I have created for Registration Model

class RegistrationForm(forms.ModelForm):

class Meta:
    model = Registration
    widgets = {
        "registration_ID": forms.NumberInput(attrs={'required': "required"}),
        "seminar": forms.SelectMultiple(attrs={'required': "required"}),
        "attendee_name": forms.TextInput(attrs={'required': "required"}),
        "email_address": forms.TextInput(attrs={'required': "required"}),
        "email_sent": forms.NullBooleanSelect(attrs={'required': "required"}),

    }
    fields = ('seminar','attendee_name','email_address','email_sent')

When I run the above RegistrationForm on my Template I get following window

Image of the Registration ModelForm

seminar field which is defined as ForeignKey in Registration Model and refers to seminarID in Seminar Model. seminar appears in the registration form pre-populated which is fine but it shows as a Seminar object (2), Seminar object (3)... Also seminarID in Seminar Model is AutoField and Primarykey as well.

When I save RegistrationForm my postgresql database doesn't get updated with the new values and selected Seminar object (*). Please advise

Both seminar table and registration table snaps are given in these links : image of seminar table and image of registration table

PhantomS
  • 420
  • 4
  • 14

1 Answers1

0

You have to add a __str__() method (for Django 3.X) to your Registration class. If you are using Django 2.X add the corresponding method __unicode__(self). Something like the folowing:

def __unicode__(self):
    return "%s (%s)"%(self.presenter_name, datetime.datetime.strftime(self.seminar_DT, "%Y-%m-%d %H:%M "))

This is to tell Django which is the string representation of a Registration object. The information is needed in order to render the object representation into the widget. Check out also the Django documentation.

gkaravo
  • 133
  • 1
  • 8