Views
- class django_faker_admin.views.FakerAdminView(model_admin: ModelAdmin, factory_class: Type[DjangoModelFactory], exclude: Tuple[str] = None, **kwargs)[source]
A view to populate dummy data for a given model.
This view integrates with the Django admin and uses a factory class to generate dummy data for a specified model. It provides a custom form within the admin interface where the user can specify the number of dummy items to create. The form excludes specified fields and ensures that only the desired number of dummy items are created.
- dispatch(request, *args, **kwargs)[source]
Handles the HTTP request and checks for permissions. This method is called before the view processes the request. It checks if the user has permission to add dummy data for the model. If not, it raises a PermissionDenied exception.
- Parameters:
request (-) – The HTTP request object.
*args (-) –
Additional positional arguments.
**kwargs (-) –
Additional keyword arguments.
- Returns:
The response object.
- Return type:
HttpResponse
- Raises:
- PermissionDenied – If the user does not have permission to add dummy data.
- exclude: Tuple[str] = None
URL to redirect to after form submission
- factory_class: Type[DjangoModelFactory] = None
Factory class to be used for creating dummy data
- form_valid(form)[source]
Handles valid form submission, creating dummy data using the factory class.
- Parameters:
form (-) – The submitted form with valid data.
- Returns:
A redirect response to the success URL.
- Return type:
HttpResponseRedirect
- get_admin_form()[source]
Creates and returns an admin form for the dummy data creation.
- Returns:
The admin form with fields for dummy data creation.
- Return type:
AdminForm
- get_context_data(**kwargs)[source]
Adds the admin form to the context data.
- Parameters:
**kwargs (-) –
Additional keyword arguments passed to the superclass method.
- Returns:
Context data including the admin form.
- Return type:
dict
- get_exclude()[source]
Constructs a tuple of field names to be excluded.
- Returns:
A tuple containing the names of fields to be excluded. This includes both fields explicitly listed in self.exclude and fields in the model marked as unique.
- get_form_class()[source]
Dynamically creates and returns a form class for dummy data creation.
This method overrides the default form to include a ‘size’ field, which specifies the number of dummy instances to be created. It also sets all fields inherited from the base form class as not required, except for the ‘size’ field which is mandatory and constrained to a range between 1 and 20.
- Returns:
- A dynamically created form class that inherits from the base form class
associated with the factory model.
- Return type:
MainForm (forms.ModelForm)
- get_form_kwargs()[source]
Returns the keyword arguments for instantiating the form. This method is used to pass the initial data and request data to the form. It includes the initial data, prefix, and any POST or PUT data if the request method is either of those.
- Returns:
A dictionary of keyword arguments to be passed to the form class.
- Return type:
dict
- get_success_message(cleaned_data)[source]
Generates a success message after creating dummy data.
- Parameters:
cleaned_data (-) – The cleaned data from the submitted form.
- Returns:
A success message indicating the number of objects created.
- Return type:
str
- get_success_url()[source]
Returns the URL to redirect to after successfully creating dummy data.
- Returns:
A URL to redirect to after form submission.
- Return type:
str
- has_add_permission(request)[source]
Checks if the user has permission to add dummy data for the model.
- Parameters:
request (-) – The HTTP request object.
- Returns:
True if the user has permission, False otherwise.
- Return type:
bool
- model_admin: ModelAdmin = None
ModelAdmin instance for the model to be populated
- template_name = 'admin/faker_admin.html'
Template name for the view