Package¶
djarg¶
-
class
djarg.
qset
(objects, *, qset=None, model=None, pk='pk', select_for_update=None)[source]¶ Lazily coerces a value into a queryset. Can be used inside of
python-args
decorators.Can coerce the following types:
Queryset
: Returns the queryset.None
: Returns an empty queryset.<primary key>
: Returns queryset with single element.Model
: Returns queryset with single model.List[<primary key>]
: Returns queryset with all elements.List[Model]
: Returns queryset with all models.
- Parameters
objects (str) – The argument name to be evaluated.
qset (QuerySet) – The queryset to use for the initial queryset when a list of values is provided.
model (Model) – The model to use for the queryset when a list of values is provided.
select_for_update (List[str], default=None) – Adds a select_for_update using the provided arguments. Does not perform select_for_update when running in partial mode. If a list is provided, the list is used as the
of
argument forselect_for_update
. If a dictionary is provided, the values are passed as kwargs toselect_for_update
. IfTrue
is provided, no arguments are passed toselect_for_update
.
Examples
Using
djarg.qset
withpython-args
arg.default
decorator:import arg import djarg @arg.defaults( profiles=djarg.qset('profiles', model=Profile).select_related('address') ) def fetch_zip_codes(profiles): return [profile.address.zip for profile in profiles] # All of these invocations can be used fetch_zip_codes(Profile.objects.all()) # A single model object fetch_zip_codes(single_profile_object) # A single PK fetch_zip_codes(1) # Lists of PKs or model objects fetch_zip_codes([2, 3]) fetch_zip_codes([Profile(...), Profile(...)])
djarg.forms¶
-
class
djarg.forms.
Field
(*args, **kwargs)[source]¶ A lazy
python-args
adapter to lazily load a Django form field.When declaring a form, a form field can be wrapped in the
Field
object so that all attributes can be lazily loaded.Example
This is an example of lazily loading a form field so that we can dynamically fill out help text:
class MyForm(forms.Form): field = Field(CharField, help_text=args.func(get_help_text))
-
djarg.forms.
adapt
(form, func, default_args=None, clean=True)[source]¶ Adapt a form to an python-args func, ensuring the form validation behaves seamlessly with function validation.
Evaluate any djarg.form.Field classes that are fields of the form.
- Parameters
form (django.forms.Form) – The Django form being adapted.
func (function) – A function decorated with
python-args
decorators.default_args (dict, default=None) – A dictionary of any other default arguments that are used when calling the
python-args
function.clean (bool, default=True) – Adapt the clean method to the validators of the func
-
djarg.forms.
get_field_validator
(func, field_label)[source]¶ Given a field label and function, generate a form field validator from the function. It is assumed that the function is wrapped with
python-args
.Django form fields need to raise ValidationErrors in order for errors to bubble up properly. This function wraps any validators for the field and re-raises ValidationErrors
djarg.views¶
-
class
djarg.views.
FormView
(**kwargs)[source]¶ A generic form view that runs a binded python-args function.
Instatiate the
FormView
similar to a regular djangoFormView
, and also declare thefunc
attribute to point to apython-args
function.The form of this view will automatically be adapted to use the validators of the function, and the
form_valid
method will run the function with the parameters from the form.
-
class
djarg.views.
MultipleObjectsMixin
[source]¶ Similar to Django’s SingleObjectMixin, but allows for pulling multile arguments through GET parameters.
Most of this code was directly adapted from
-
get_default_args
()[source]¶ Return any arguments that should be sent to the function and made available during lazy form field loading.
-
get_objects
()[source]¶ Return the objects the view is displaying. Require
self.queryset
and apk
argument in the GET query string. Subclasses can override this to return any object.
-
get_queryset
()[source]¶ Returns the queryset for multi-object views. If the queryset as been overridden to be a “lazy” function (e.g. from
python-args
), the function will be evaluated withrequest
as a keyword argument and returned. Otherwise, it defaults to Django’s generic detail-viewget_queryset()
function.
-
-
class
djarg.views.
ObjectFormView
(**kwargs)[source]¶ A generic view for single objects that runs a binded python-args function.
Similar to
FormView
, the form of this view will automatically be adapted to use the validators of the function, and theform_valid
method will run the function with the parameters from the form.
-
class
djarg.views.
ObjectWizardView
(**kwargs)[source]¶ A WizardView that operates on a single object.
-
class
djarg.views.
ObjectsFormView
(**kwargs)[source]¶ A generic view for multiple objects that runs a binded python-args function.
Similar to
FormView
, the form of this view will automatically be adapted to use the validators of the function, and theform_valid
method will run the function with the parameters from the form.
-
class
djarg.views.
ObjectsWizardView
(**kwargs)[source]¶ A WizardView that operates on multiple objects.
-
class
djarg.views.
SessionObjectWizardView
(**kwargs)[source]¶ An ObjectWizardView with pre-configured SessionStorage backend.
-
class
djarg.views.
SessionObjectsWizardView
(**kwargs)[source]¶ An ObjectsWizardView with pre-configured SessionStorage backend.
-
class
djarg.views.
SessionWizardView
(**kwargs)[source]¶ A WizardView with pre-configured SessionStorage backend.
-
class
djarg.views.
SingleObjectMixin
[source]¶ -
get_default_args
()[source]¶ Return any arguments that should be sent to the function and made available during lazy form field loading.
-
get_queryset
()[source]¶ Returns the queryset for single-object views. If the queryset as been overridden to be a “lazy” function (e.g. from
python-args
), the function will be evaluated withrequest
as a keyword argument and returned. Otherwise, it defaults to Django’s generic detail-viewget_queryset()
function.
-
-
class
djarg.views.
SuccessMessageMixin
[source]¶ Similar to Django’s SuccessMessageMixin, allows views to add a success message when successfully finished.
Users can override the
success_message
attribute or override theget_success_message(self, args, results)
method. The latter takes the arguments provided to the main viewfunc
attribute and the results from running the viewfunc
.
-
class
djarg.views.
WizardView
(**kwargs)[source]¶ Adaptation of django-formtool’s wizard view.
Children of this class, such as
SessionWizardView
, can use lazy evaluation methods present in python-args.-
done
(*args, **kwargs)[source]¶ This method must be overridden by a subclass to process to form data after processing all steps.
-
get_cleaned_data
(*steps)[source]¶ Gets cleaned data for all steps in order given.
If any steps don’t validate, return None
-
get_form_list
(until=None)[source]¶ Overrides get_form_list() to dynamically evaluate arg.func() conditions. Allows conditional evaluation up to a specific step so that we avoid various infinite recursion issues.
-