[Answer]-"This field is required." when sending JSON using POST with Django REST Framework

1đź‘Ť

âś…

Posting this for the potential that other people might experience this issue which seems to be pertaining to it being a legacy DB. The problem lies in the fact that the PK field in “tskmail” table is also a FK to “tskmst”.

See Multi-Column Primary Key support for more info.

What I ended up doing is making another set of models strictly for PUT that has no FK relationship but is strictly an Integer Field.

class TskmailPOST(models.Model):
    tskmail_id = models.IntegerField(primary_key=True)
    tskmail_to_int = models.TextField(blank=True)
    tskmail_to_ext = models.TextField(blank=True)
    tskmail_subject = models.TextField(blank=True)
    tskmail_memo = models.TextField(blank=True) # This field type is a guess.
    tskmail_priority = models.SmallIntegerField(blank=True, null=True)
    tskmail_attach = models.TextField(blank=True)
    class Meta:
        managed = False
        db_table = 'tskmail'

Instead of –

class TskmailPOST(models.Model):
    tskmail_id = models.ForeignKey(Tskmst, db_column='tskmail_id', primary_key=True)
    tskmail_to_int = models.TextField(blank=True)
    tskmail_to_ext = models.TextField(blank=True)
    tskmail_subject = models.TextField(blank=True)
    tskmail_memo = models.TextField(blank=True) # This field type is a guess.
    tskmail_priority = models.SmallIntegerField(blank=True, null=True)
    tskmail_attach = models.TextField(blank=True)
    class Meta:
        managed = False
        db_table = 'tskmail'

Then I had to call 2 separate serializers and using the POST data, break it into 2 separate dict files and validate the first, load it then validate the second and load it.

Leave a comment