0

I have a schema of 2 classes:

class Anomaly(DjangoObjectType):
    class Meta:
        model = models.Anomaly


class Batch(LoggedModel):
    class Meta:
        model = models.Batch

where LoggedModel is

class LoggedModel(DjangoObjectType):
    class Meta:
        model = models.LoggedModel

originally both inherited from DjangoObjectType but then neither of the two was exposing a field logs defined in the superclass of the model

class LoggedModel(models.Model):
    recursive_field = "parent_model"
    logs = models.ManyToManyField(Log, related_name="%(app_label)s_%(class)s")

    class Meta:
        abstract = True

class Anomaly(LoggedModel):
    ...

class Batch(LoggedModel):
    anomalies = models.ManyToManyField(Anomaly)

Now, after declaring in the schema Batch a subclass of LoggedModel instead of DjangoObjectType, logs is exposed for both classes. Anyone can shed some light?

Daniele Bernardini
  • 1,516
  • 1
  • 12
  • 29

1 Answers1

1

Any related fields that you wish to have in your models need to be explicitly declared, as you've done in your second example. In the first example, logs isn't automagically exposed even though it exists, I assume, in the model definition.

In the second, it is exposed in LoggedModel, thus also for Anomaly and Batch.

You should find that they both work simply as DjangoObjectTypes if you explicitly expose logs in them.

Take a look here: https://stackoverflow.com/a/56173485/214150

Chris Larson
  • 1,684
  • 1
  • 11
  • 19
  • Just to clarify the above is the code that is running right now. The upper part is the schema and the lower part is the model. `logs` is a field in the abstract model `LoggedModel`. If I now change just the inheritance for `Batch` from `LoggedModel` to `DjangoObjectType` it stops working. – Daniele Bernardini May 17 '19 at 06:51