2

I'm trying to create an instance of this Report model:

class Report(models.Model):
    """
    A model for storing credit reports pulled from Equifax.
    """
    user = models.ForeignKey(to=CustomUserModel, on_delete=models.CASCADE,
                             help_text='User report belongs to.')

    timestamp = models.DateTimeField(default=timezone.now)
    report = JSONField()

However, whenever I try I get this error:

Exception Type: TypeError at /internal/report
Exception Value: 'report' is an invalid keyword argument for this function

This happens whether I instantiate the instance using the Report().save() method, or the Report.object.create() method as follows:

    report_obj = Report.objects.create(
        user=user,
        report=report
    )

Does anyone have any clue what's going on? There is very clearly a "report" attribute for that class, so why the error?

Thanks!

Soviero
  • 1,774
  • 2
  • 13
  • 23

1 Answers1

0

Based on the the error and the comment:

(...) Looks like I imported the form field from DRF instead of the model field of the same name from Django (...)

You did not import a JSONField that is a model field, but something else (for example a form field, or here a DRF field). As a result, Django does not consider report to be a field of your Report module, it sees it as a "vanilla" Python attribute.

You thus should make sure that JSONField links to the model field class instead. Adding such field will probably result in another migration to add the field to the database table:

from django.contrib.postgres.fields import JSONField

class Report(models.Model):
    """
    A model for storing credit reports pulled from Equifax.
    """
    user = models.ForeignKey(to=CustomUserModel, on_delete=models.CASCADE,
                             help_text='User report belongs to.')

    timestamp = models.DateTimeField(default=timezone.now)
    report = JSONField()
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555