Domanda

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?

È stato utile?

Soluzione

Set you null=True inside your History class.

class History(models.Model):  
    uid = models.ForeignKey(User, to_field='username', db_column='uid', null=True)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top