I have to work with a legacy database and have the following problem. My model has a foreign key to the User Model:

class History(models.Model):
    uid = models.ForeignKey(User, to_field='username', db_column='uid')

That seems to work. The Problem is that for some History entries the User entry doesn't exist anymore. Is there an easy solution?

The Application seems to work fine with it. The only problem is in the Admin-Interface of the Model:

class HistoryAmdin(admin.ModelAdmin):
    list_display = ('id', 'uid')

It shows only entries with a valid foreign key. Is it possible to show also the other entries?

有帮助吗?

解决方案

Set you null=True inside your History class.

class History(models.Model):  
    uid = models.ForeignKey(User, to_field='username', db_column='uid', null=True)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top