Comments (5)
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.
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.
there is related bug #1016 that I cannot reproduce
maybe you can show me full listing for the Referrer
model ?
from django-ninja.
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.
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)
- Overriding JSON schema title and summary without using `Field` HOT 1
- [BUG] CSRF Fails with Django Auth HOT 16
- [BUG] Order of operations affect response and request schemas HOT 1
- Documentation - link the schema article to the error article
- Add description to tags HOT 2
- Field order issue in JSON response HOT 1
- [BUG] Allow specifying field types for custom fields
- [BUG] The other API URLs work, but the openapi docs don't appear. HOT 5
- [BUG] Can't use parameter `keys`
- how to add a description for query params that shows in the generated docs?
- Field with list factory causes API docs to not load with non-serializable. HOT 2
- Annotate Decimal field does not supported "Unable to serialize unknown type: <class 'ellipsis'"
- Traversing the data structures to create an HTTP API client HOT 3
- how to use token ? HOT 1
- Path parameters, order matters HOT 3
- [BUG] Combining JWTAuth with django_auth only works in that order
- [BUG] Pydantic 2.7.0 incompatibily HOT 4
- [BUG] ModelSchema with ManyToManyField won't work under async views HOT 5
- How do you use aliases with nested objects
- [BUG] HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-ninja.