vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.03k stars 421 forks source link

Django model id field is not recognized for ModelSchema #1298

Closed twkrol closed 1 week ago

twkrol commented 1 week ago

Hi I'm using django-ninja for my project, so far it worked well. But today I've added a new django app called favorites, then created the model, model schema and got this:

ninja.errors.ConfigError: DjangoField(s) {'id'} are not in model <class 'favorites.models.Favorite'>

The model is quite simple:

class Favorite(models.Model):
    name = models.CharField(max_length=1024)
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    materials = models.ManyToManyField(Material, verbose_name=_("List of materials"), blank=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self) -> str:
        return f"{self.name}"

The schema definition is:

from ninja import ModelSchema

from favorites.models import Favorite

class FavoriteOutSchema(ModelSchema):
    class Meta:
        model = Favorite
        fields = ("id", "name", "materials", "created", "updated")

For previously created django models, the schema creation works well, no complains about non existing id field (id and pk fields are inherited from djano model base class).

I don't understand why it refuses to create schema for this new model!? Any ideas?

django version: 5.0.8 django-ninja version: 1.3.0

twkrol commented 1 week ago

ok, found bug in my model, closing