whats the difference between Django models and forms? - django

I am new to Django and can't understand the models and forms. Can any one suggest me the differences and tutorials related to them.

Basically, a model encapsulates information about something (i.e., it models it), and is stored in the database. For example, we could model a person:
from django import models
class Person(models.Model):
name = models.CharField(max_length=100)
age = models.PositiveIntegerField()
height = models.FloatField()
weight = models.FloatField()
Whenever a model instance is created and saved, Django stores it in the database for you to retrieve and use at a later date.
On the other hand, forms correspond to HTML forms, i.e., a set of fields which are presented to the end user to fill some data in. A form can be completely independent of a model, for example a search form:
from django import forms
class SearchForm(forms.Form):
search_terms = forms.CharField(max_length=100)
max_results = forms.IntegerField()
When submitted, Django takes care of validating the values the user entered and converting them to Python types (such as integers). All you then have to do is write the code which does something with these values.
Of course, if you have created a model, you will often want to allow a user to create these models through a form. Instead of having to duplicate all the field names and create the form yourself, Django provides a shortcut for this, the ModelForm:
from django.forms import ModelForm
class PersonForm(forms.ModelForm)
class Meta:
model = Person
As for further reading, I would start with the Django documentation, which includes a tutorial on creating and using models, and a fairly in-depth look at forms. There are also plenty of Django books and online tutorials to help you along.

Models are related to the database abstraction layer covered in Tutorial 1:
http://docs.djangoproject.com/en/dev/intro/tutorial01/
It covers everything from what they are, what the philosophy is, what it's abstracting (raw sql). Read it and come back if you have any questions, because it's really good.
Tutorial 4 covers forms.
http://docs.djangoproject.com/en/dev/intro/tutorial04/
The forms framework is just a helper for HTML forms. There are also ModelForms, based on the forms framework, that ties models together with forms, but the core of it is a framework for dealing with HTML form display, validation, and processing.

Related

Django Wagtail forms based on models

I have a simple model that references my Auth_User_model. I want to create a simple form that creates an OAuth2Client which inherits from functionality from OAuth2ClientMixin.
class OAuth2Client(models.Model, OAuth2ClientMixin):
user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
class FormPage(AbstractForm):
class Meta:
model = OAuth2Client
What I am unclear about is how to bind my Form Page to my model? I'm using this high level tutorial but seems a bit geared to the AbstractEmailForm. Which doesn't work for me. How do I create a form based on a model?
The wagtail.contrib.forms app is geared towards simple data collection such as surveys, where the form fields are defined editorially through the admin interface - this isn't the case here, where the form fields are inherently tied to the model definition set up in code. A Django ModelForm is a better fit:
from django.forms import ModelForm
class OAuth2ClientForm(ModelForm):
class Meta:
model = OAuth2Client
If you want to incorporate this form into a Wagtail page rather than a plain Django view, you can override the serve method on the page model, which has very similar behaviour to a Django view function (taking a request object as an argument, and returning a response object) - this is where you would put the logic for checking request.POST and processing or rendering the form accordingly.

Django User-Interests App and its database Model design

I'm am building a django app which takes user Interests as inputs.
Now I have 2 Questions -
First is that, what model should I use, should I just add a field to user model or a separate Interest Model and link via Foreign Key?
I know the former design is bad, and so I.m trying latter one, I'm having a hard time in Django to create Interest Model and its view to save the user interests.
Any help is appreciated.
I am trying to accomplish the same thing.
Here is how I have solved it:
I have not tried it out yet, but this should work as a solution.
from django.contrib.auth.models import User
class Nation(models.Model):
name=models.CharField(max_length=64)
class Subject(models.Model):
name=models.CharField(max_length=64)
class Interests(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
nationals = models.ManyToManyField(Nation)
subjects = models.ManyToManyField(Subject)

create model formsets from same model but with different model forms

I have an Image model in my Django project. Because of different types of Image I have created three ModelForms according to each type:
class Xray(ModelForm):
#extra_field: Choice Field with specific options for Xray
class Meta:
model = Image
class Internal(ModelForm):
#extra_field: Choice Field with specific options for Internal
class Meta:
model = Image
class External(ModelForm):
#extra_field: Choice Field with specific options for External
class Meta:
model = Image
Each ModelForm has a save logic implemented. I want to create a model formset one for each Image type but want to use the correct ModelForm for each type of Image. I won't use this formset for editing thus I always want it to be empty and have 5 forms(5 items). I can't seem to find in django docs where i can use a specific form for a formset. Only a specific formset (inherit from BaseModelFormSet)
Is it possible to use specific form for each model_formset?
You can do the following:
from django.forms.models import modelformset_factory
from someproject.someapp.models import Image
from someproject.someapp.forms import Internal
ImageFormSet = modelformset_factory(Image, form=Internal)
Here are the docs for modelform_factory, which don't mention the form argument. However, in the "Note" below the examples therein that the function delegates to formset_factory, which is documented to take the form argument. It's just a minor docs issue, and might be a good reason to create a fork of Django, make an update to the docs, and create a pull request.

How to sync OnetoOne relation in CreateView

models:
class Book(models.Model):
name = models.Char()
class BookCount(models.Model):
book = OneToOneField(Book)
count = SmallIntegerField(default=0)
views:
class BookCreate(CreateView):
model = Application
The question is, after create Book, I want insert a record to BookCount. Any ideas?
If your BookCount model is required you could use a listener for the post_save signal along the lines of this:
# models.py
from django.db.models.signals import post_save
# Model definitions
...
def create_book_count(sender, instance, created, **kwargs):
if created:
BookCount.objects.create(book=instance)
post_save.connect(create_book_count, sender=Book)
If your models are really this simple, you may want to drop the BookCount model and add a count field to your Book model instead to reduce the complexity and overhead here. See the docs on extending the user model for a short overview of why it might be better to avoid the OneToOneField option (the wording is specific to the User model, but it applies here, too):
Note that using related models results in additional queries or joins to retrieve the related data, and depending on your needs substituting the User model and adding the related fields may be your better option. However existing links to the default User model within your project’s apps may justify the extra database load.

Django form with ManyToMany field with 500,000 objects times out

Lets say for example I have a Model called "Client" and a model called "PhoneNumbers"
class PhoneNumbers(models.Model):
number = forms.IntegerField()
class Client(models.Model):
number = forms.ManyToManyField(PhoneNumbers)
Client has a ManyToMany relationship with PhoneNumbers. PhoneNumbers has almost 500,000 records in it so when it comes to editing a Client record from a model form with a MultiSelect widget that comes with a M2M filed, it takes forever to load. In fact, it never does. It just sits there trying to load all of those phone objects I am assuming.
My workaround was to so some tedious things with ajax and jquery to edit only the phone numbers in a Client record. Before wasting my time with all of that I wanted to see if there is somehow another way to go about it without having my page hang.
You need to create a custom widget for this field that lets you autocomplete for the correct record. If you don't want to roll your own: http://django-autocomplete-light.readthedocs.io/
I've used this for its generic relationship support, the M2M autocomplete looks pretty easy and intuitive as well. see video of use here: http://www.youtube.com/watch?v=fJIHiqWKUXI&feature=youtu.be
After reading your comment about needing it outside the admin, I took another look at the django-autocomplete-light library. It provides widgets you can use outside the admin.
from dal import autocomplete
from django import forms
class PersonForm(forms.ModelForm):
class Meta:
widgets = {
'myformfield': autocomplete.ModelSelect2(
# ...
),
}
Since Django 2.0, Django Admin ships with an autocomplete_fields attribute that generates autocomplete widgets for foreign keys and many-to-many fields.
class PhoneNumbersAdmin(admin.ModelAdmin):
search_fields = ['number']
class ClientAdmin(admin.ModelAdmin):
autocomplete_fields = ['number']
Note that this only works in the scope of Django admin of course. To get autocomplete fields outside the admin you would need an extra package such as django-autocomplete-light as already suggested in other answers.
Out of the box, the model admin has a raw_id_fields option that let your page load much quicker. However, the user interface of raw id fields isn't very intuitive, so you might have to roll your own solution.
We use this 3rd party widget for this:
https://github.com/crucialfelix/django-ajax-selects
Btw, your 'example' above is really bad DB design for a bunch of reasons. You should just have the phone number as a text field on the Client model and then you would have none of these issues. ;-)