Code Monkey home page Code Monkey logo

Comments (5)

pv-jeff avatar pv-jeff commented on May 29, 2024 1

@vitalik
@jaeyongjaykim

I've run into the same error: TypeError: cannot specify both default and default_factory

It's on a Django Model field that has a callable default and is listed as optional in the ModelSchema

The ModelSchema I'm using, in it's Meta, uses the model, fields and fields_optional attributes.
If I remove the offending field from the fields_optional list, the error goes away.

In the ninja/orm/fields.py file, if I change line 157 from
if optional:
to
if optional and not default_factory:

The error no longer occurs.

From what I can see, there is a path in fields.py that allows default to be set to None and default_factory to be set as well. Pydantic sees default=None as default being set, and therefore creates the error condition.

from django-ninja.

spectorar avatar spectorar commented on May 29, 2024 1

I am running into this same issue and can confirm that my field has a callable default value. Is there any workaround for this?

from django-ninja.

vitalik avatar vitalik commented on May 29, 2024

@jaeyongjaykim

there is related bug #1016 that I cannot reproduce

maybe you can show me full listing for the Referrer model ?

from django-ninja.

jaeyongjaykim avatar jaeyongjaykim commented on May 29, 2024

@vitalik

Here you are...

class Referrer(BaseModel):
    user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
    landing_dt = models.DateTimeField(null=True, blank=True, help_text="The time when the user arrived at the target page via deep link")
    utm_source = models.CharField(max_length=255, null=True, blank=True, help_text="Referrer")
    utm_medium = models.CharField(max_length=255, null=True, blank=True, help_text="Marketing medium")
    utm_campaign = models.CharField(max_length=255, null=True, blank=True, help_text="Product, promotion code, slogan, etc.")
    utm_term = models.CharField(max_length=255, null=True, blank=True, help_text="Product keyword")
    utm_content = models.CharField(max_length=255, null=True, blank=True, help_text="Ad content")
    media_source = models.CharField(max_length=255, null=True, blank=True, help_text="AppsFlyer advertising media")
    campaign = models.CharField(max_length=255, null=True, blank=True, help_text="AppsFlyer advertising campaign")
    link = models.URLField(max_length=1000, null=True, blank=True, help_text="AppsFlyer advertising link")
    deep_link_value = models.CharField(max_length=255, null=True, blank=True, help_text="AppsFlyer deep link")
    deep_link_sub1 = models.CharField(max_length=255, null=True, blank=True, help_text="Additional value for AppsFlyer deep link")
    raw_data = models.JSONField(blank=True, default=dict, help_text="AppsFlyer raw data")

    class Meta:
        verbose_name = "Referrer"
        verbose_name_plural = "Referrers"

    def __str__(self) -> str:
        """Stringify the object of this model.
        This method is used in Django admin page.
        """
        return f"id: {self.id} user: {self.user}"

from django-ninja.

joseph-sabido avatar joseph-sabido commented on May 29, 2024

I've gotten around it by adding the field as a class attribute instead of the list

If this gives you the error:

class SomeSchema(ModelSchema):
    class Meta:
        model = SomeModel

        fields = [
            'field_with_callable_default',
            'field2',
            'field3',
        ]

        fields_optional = [
            'field_with_callable_default',
        ]

Replace with the following

class SomeSchema(ModelSchema):
    field_with_callable_default: Optional[int] = None

    class Meta:
        model = SomeModel

        fields = [
            'field2',
            'field3',
        ]

from django-ninja.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.