Comments (5)
This doesn't seem possible.
The form field and the widget is associated with a model field only once when the django process starts. Therefore, the widget has no way of accessing the model instance.
from django-jsonform.
Okay, I'll add this. Seems like this is the best that can be done about this feature.
from django-jsonform.
I've released this feature as per your suggestions in version 2.8.0.
Thank you for this.
from django-jsonform.
Hi there,
I've just figured out how to implement this.
You are right, by default, it is really not possible to access model instance during the widget render process but it is possible to pass it if you have custom model admin form - which is highly likely - and if not, it's really not hard to create one.
# admin.py
class FooAdminForm(forms.ModelForm):
class Meta:
model = Foo
exclude = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['bar'].widget.instance = self.instance # passing instance to the field called bar
@admin.register(Foo)
class FooAdmin(admin.ModelAdmin):
form = FooAdminForm
So how about giving the possibility for developers to access the instance if they really need it by just take look if there is an instance attribute available in widgets.JSONFormWidget during the render method and pass it to shcema callable if there it is:
# django_jsonform/widgets.py
class JSONFormWidget(forms.Widget):
# from line 17
def render(self, name, value, attrs=None, renderer=None):
if callable(self.schema):
instance = getattr(self, 'instance') if hasattr(self, 'instance') else None
schema = self.schema() if instance is None else self.schema(instance)
else:
schema = self.schema
from django-jsonform.
Sounds promising! Thank you!
from django-jsonform.
Related Issues (20)
- Object of type UUID is not JSON serializable HOT 1
- New release HOT 1
- Custom validation of object properties not showing error next to field HOT 2
- Form dynamics not working after reloading django-jsonform with HTMX HOT 3
- Using an object with empty Properties, having all properties inside AdditionalProperties HOT 3
- Setting JSONField schema dynamically HOT 3
- Copy-pasting / Duplications for Array items and Objects
- Horizontal display of properties HOT 1
- Dynamic header generation for array items HOT 2
- Customising "Add key" and "Add item" texts HOT 1
- Add button to open link in URLField HOT 5
- Decimal values not allowed when entering integer minimum and maximum for 'number' types HOT 4
- Define defaults for an array HOT 3
- Subfields in ArrayField should get the relevant input type
- Issue with errors for fields inside an anyOf schema not displaying correctly HOT 1
- validators for jsonfield are not updated with dynamic schema HOT 3
- Cooperation with other repository `django-pydantic-field` for model schema validation HOT 1
- Title is not populated for `oneOf` when referenced through `$ref` HOT 4
- tests dir in .venv broke project tests HOT 1
- JSONFieldWidget attrs 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-jsonform.