Giter VIP home page Giter VIP logo

django-user-fields's Introduction

django-user-fields

Total downloads PyPI version GitHub release publish action

A Django application that makes user creation of form fields simple, handling everything from form generation to data storage and retrieval.

This was initially built for MUN Manager to allow conferences to collect whatever information they wanted to during all parts of the registration process. django-user-fields allows conferences to easily add their own extra fields to various forms using a simple admin interface creating a tailored experience.

Simple Installation and Usage Guide

  1. Install django-user-fields:
$ pip install django-user-fields
  1. Add user_fields to your installed apps:
INSTALLED_APPS = (
    ...
    'user_fields',
    ...
)
  1. Run a database migration:
$ python manage.py migrate
  1. Create an ExtendedExtraField model:
from user_fields.models import ExtraField

class ExtendedExtraField(ExtraField):

    # You can add any custom fields you'd like here to make filtering fields easier.
    # A list of all predefined fields can be found below.
  1. Add the @initialise_extra_fields decorator to your form:
from user_fields.decorators import initialise_extra_fields

@initialise_extra_fields
class ExampleForm(forms.ModelForm):
  1. Add the UserFieldMixin to the object(s) you would like to have extra fields:
from user_fields.mixins import UserFieldMixin

class ExampleObject(models.Model, UserFieldMixin):
  1. Add a UserDataField to the object for storage and migrate:
from user_fields.models import UserDataField, ExtraField

class ExampleObject(models.Model, UserFieldMixin):

    extra_data = UserDataField()

$ python manage.py migrate

Note: If extra_data is taken and/or you would like to name the storage field something else, you can define USER_FIELDS_ATTR_NAME in your settings.

  1. Create and pass some extra fields to your form:
extra_fields = ExtendedExtraField.objects.filter(parameter=something)

form = ExampleForm(... extra_fields=extra_fields, ...)
  1. Add an extra line to save the extra fields to your object:
object = form.save()
object.save_extra_form_data(form) # Ensures that both object and form save functions are not overwritten.

Supported Fields

  • CharField
  • CharField With Choices
  • TextField
  • BooleanField
  • EmailField

Feel free to submit a pull request if you would like to add more fields. Most fields should be relatively easy to add, although FileField will be an interesting challenge!

ExtraField Attributes

Proper Name Name Description Example
Name name Equivalent to the HTML input name parameter. example-field (slug format)
Label label Django form field label (rendered name). Example Field
Help Text help_text Django form field help text. This is some guidance.
Required required Django form field required parameter. True / False
Field Type field_type Dropdown with all supported fields. CharField (char)
Max. Length max_length CharField (only) max length. 35
Choices choices Choices for the CharField With Choices. Option 1,Option 2,Option 3

UserFieldMixin Functions

.retrieve_extra_data(ExtraField, formatted=True/False): Returns the data stored for a given field. If formatted is True, it will return the value for the CharField With Choices, otherwise it will return the key of the choice.

.save_extra_data(ExtraField, data): Saves the data supplied for a given field to the object.

.save_extra_form_data(Form): Saves all of the extra field data in a form to the object.

.delete_extra_data(ExtraField): Deletes all of the data associated with a given field.

To Do

  • Improve documentation.
  • Implement testing.
  • Add support for more fields.

django-user-fields's People

Contributors

rishk avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.