Django Foreign Fields
Django Foreign Fields is a Django app that offers two new form fields that help handling foreign relationships in a easier way.
Quick start
bash pip install django-foreign-fields
Usage
Django-foreign-fields adds two new form fields ForeignField and TextAreaToManyField.
Fields
Both form fields need two new arguments: to and selector.
-
to: the target model of the relationship. Declaring the target model as the first parameter will automatically be detected as the to argument. -
selector: the model field where the given value will be searched on.
ForeignField
Receives data and searches for an object in database that has that unique value on the given field. The object is stored as a ForeignKey.
TextAreaToManyField
Breaks all lines in a Textarea and use each one to find objects that correspond to each search. Each search must return a unique object, then, all objects are stored in a ManyToMany relationship.
Examples
ForeignField
Given you already have two models, one Target that holds some information and one Referrer that will make a foreign relationship to a particular field:
class Target(models.Model):
name = models.TextField()
class Referrer(models.Model):
foreign = models.ForeignKey()
It will be needed a new form for the Referrer.
The original form field that holds the foreign key relation on the Referrer will be substituted for the ForeignField:
import foreign_fields.ForeignField
class ReferrerForm(forms.ModelForm):
foreign = foreign_fields.ForeignField(to=Target, selector='name')
class Meta:
model = Referrer
Now you can use the form on your View. The default widget is TextInput, so when you enter a string in the field and save the form, the ForeignField will search for a Target that has the given string in it's name field that must be unique. If the field is not unique, it will be given a ValidationError.
It's possible to change the foreign widget into others such as NumberInput or DateInput.
TextAreaToManyField
Given you already have two models, one Target that holds some information and one Referrer that will make a many to many relationship to a particular field:
class Target(models.Model):
name = models.TextField()
class Referrer(models.Model):
many_to_many = models.ManyToManyField()
It will be needed a new form for the Referrer.
The original form field that holds the many to many relation on the Referrer will be substituted for the TextAreaToManyField:
import foreign_fields.TextAreaToManyField
class ReferrerForm(forms.ModelForm):
many_to_many = foreign_fields.TextAreaToManyField(to=Target, selector='name')
class Meta:
model = Referrer
Now you can use the form on your View. The default widget is Textarea, so when you enter a text in the field and save the form, the TextAreaToManyField will split each line and search for a unique Target by line that has the given string in it's name field. If the field is not unique, it will be given a ValidationError.
Metadata
Release files for django-foreign-fields 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_foreign_fields-0.3.2.tar.gz | 5.6 kB | Without provenance | Details for django_foreign_fields-0.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_foreign_fields-0.3.2-py3-none-any.whl | Python 3 | none | any | Details for django_foreign_fields-0.3.2-py3-none-any.whl |
Total release size: 12.4 kB
Release files / django_foreign_fields-0.3.2.tar.gz
| Download URL | django_foreign_fields-0.3.2.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
710f36bfcbfd7fb2a807dae830978d0fe56416c9f13c5c10caf4413f6ff639ca
|
|
BLAKE2b-256 checksum How to use checksums |
5c97c43722d02472b17897451bee424844f123a4b57265946aaa42c4d19e439d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.19
|
Release files / django_foreign_fields-0.3.2-py3-none-any.whl
| Download URL | django_foreign_fields-0.3.2-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f1880aafc897f1ccc7d1ac79ed0c7184fb50069fdd416fef8c694e42bf694ae1
|
|
BLAKE2b-256 checksum How to use checksums |
bd0be637d1cdd5e9234897baebf3b4ccbc287393ad4a52e88e2eac03c3998980
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.19
|
Security for django-foreign-fields 0.3.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.