0

I have the following models:

class BaseModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

    class Meta:
        abstract = True

class Content(BaseModel):
    title = models.CharField(max_length=2000)

class Document(Content):
    file = models.FileField()
    reading_time = models.IntegerField()

class DocumentView(BaseModel):
    document = models.ForeignKey(Document, on_delete=models.CASCADE)
    viewed_on = models.DateTimeField(auto_now=True)

Everything works as expected, except that in the Django Admin, editing a DocumentView model, the Document dropdown doesn't get selected with the saved Document.

Regarding the Admin, I use it in the default way:

admin.site.register(DocumentView)

Here's a screenshot: enter image description here

Tuk
  • 143
  • 2
  • 10
  • can you add a screenshot too ? – JPG Mar 01 '18 at 03:47
  • There's a screenshot – Tuk Mar 01 '18 at 05:27
  • I removed "Training" field from the code example for clarity – Tuk Mar 01 '18 at 05:28
  • `viewed_on` is on admin site!! How could that possible if `auto_now=True`. Am I wrong? (not related to your question, though) – JPG Mar 01 '18 at 05:35
  • 1
    If that `Document` drop-down has no elements, it means you don't have any `Document` instances currently in `DB`. To create new `Document` instance **via Django Admin**, use that **+** button, and add info related to your `Document` model. Did this solve your issue? – JPG Mar 01 '18 at 05:39
  • Hi Jerin, thanks for your answer. Actually, a few `Documents` exists already and are loaded on the dropdown. The screenshot was taken from an "edit" screen, not a "create", so this `DocumentView` already exists on the DB and has a Document assigned. The problem is that when I go to edit that `DocumentView`, the dropdown does not select the right `Document` and shows blank. – Tuk Mar 01 '18 at 05:44
  • This problem started happening when I created the "Content" base model – Tuk Mar 01 '18 at 05:45

0 Answers0