[Answer]-Django rest framework – POST when Primary Key is also a Foreign Key – "This field is required." OR 'RelatedManager' object is not iterable

1👍

Django doesn’t like it if you don’t have an integer PK field. I think it also runs into conflicts if the name of a ForeignKey field is the same as its db_column. In this case the legacy DB design makes it hard. In addition to the workaround you describe:

I think I may have “tricked” the system. what I did was create a new field called tskmail = models.IntegerField(db_column='tskmail_id', primary_key=True) and I removed the primary_key=True from the ForiegnKey so essentially I’m displaying the field twice.

I might try leaving tskmail_id declared as an integer PK field and declare tskmail as a ForeignKey with db_column='tskmail_id' (or leave db_column unspecified so that default choice applies). It might be worth checking how Django implements subclassed non-abstract models – they might have a PK field that is also a FK to the parent table.

Leave a comment