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:

  1. Queryset: Returns the queryset.

  2. None: Returns an empty queryset.

  3. <primary key>: Returns queryset with single element.

  4. Model: Returns queryset with single model.

  5. List[<primary key>]: Returns queryset with all elements.

  6. 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 for select_for_update. If a dictionary is provided, the values are passed as kwargs to select_for_update. If True is provided, no arguments are passed to select_for_update.

Examples

Using djarg.qset with python-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.forms.get_form_clean(func, form, default_args=None)[source]

Returns a form clean method for the form, using any validators present on the python-args wrapped func.

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 django FormView, and also declare the func attribute to point to a python-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_context_data(**kwargs)[source]

Insert the single object into the context dict.

get_context_objects_name(objects)[source]

Get the name to use for the object.

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 a pk 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 with request as a keyword argument and returned. Otherwise, it defaults to Django’s generic detail-view get_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 the form_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 the form_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 with request as a keyword argument and returned. Otherwise, it defaults to Django’s generic detail-view get_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 the get_success_message(self, args, results) method. The latter takes the arguments provided to the main view func attribute and the results from running the view func.

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_cleaned_data_for_step(step)[source]

Get cleaned data for a specific step.

get_form(step=None, **kwargs)[source]

Get a form for a specific step

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.

render_done(form, **kwargs)[source]

Overrides render_done and shows a revalidation failure if any errors happened.

run_func()[source]

Run the primary function. This should be called from the “done()” method that is overridden by users.