I use the Django built-in models.User as basic user model, plus a model called Profile containing other user information, which has a one-to-one field relation to User.
class Profile:
user = models.OneToOneField(User)
nickname = models.CharField()
bio = models.CharField
I would like to use modelform for both model in the register view.
And in the register page, the html form literally contains field in both User and Profile, and this form will be submitted with all params in the POST, which means in the view function I have to handle two modelform with one POST params dictionary.
I have no idea how to implement this view function.
Related
I have 4 models: Blogger, User, Post and Comment.
Here is the blogger model.
class Blogger(models.Model):
username = models.OneToOneField(User, related_name='bloggers')
blogger_bio = models.CharField(max_length=1000)
Now, I want to display username and blogger_bio depending to the URL.
For example, is someone is using: /testuser, the template will filter username and blogger_bio of only user named testuser.
How to filter this Dynamically?
Use URL dispatcher in django to pass the username to your views. You can render the response template as you wish through views. Try it and post if there are any issues.
https://docs.djangoproject.com/en/1.8/topics/http/urls/
I'm migrating something from an old PHP/apache server to Django. I'm a bit stumped with the 'ModelForm'.
As far as I understand, a "Model" is the abstraction for persistent elements in my website/server - specifically this is something stored physically, say in a database, and defines the fields (read columns) in the DB.
I started moving the authentication part of the site, and discovered models, and specifically the User model (I made an empty User inheriting AbstractUser just in case I will ever need to extend things). Now I want to create a simple two field form, to authenticate login.
The form:
Username (which is a field of User, by default)
Password (Which is not).
Even the 'Username' needs a redefinition in the model form. So my questions:
What is the advantage of the model form (over just a form)? - seems like you're redefining fields anyway, and obviously sometimes adding fields on top of the model.
Specifically for authentication, I probably need to store my salted hash associated with the user somehow, compare my password using that and retrieve the user object. This is something I find very hard to find in the Django docs - they just have too much written on authentication, and not one full code example. Do I put this in the "validate" method of form, retrieving there an object and storing it in a session or something?
If there is a deeper relation between a model form and the associated model, I would like to know as well.
Simple django forms and modelforms have quite differences.
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['pub_date', 'headline', 'content', 'reporter']
The above example illustrates that you don't have to write any form field in here. The model form will itself create a form which is based on the attributes provided in the model ('Article' in this example).
If you create a simple django form then it would be something like:
class ArticleForm(forms.Form):
some_field = forms.CharField(some_attrs)
...
The django User model provides you everything you need for authentication. When you want to create users just import django.contrib.auth.models.User and use create method to create objects. Then when you want to authenticate a user use authenticate method.
from django.contrib.auth import authenticate, login
def user_login(request):
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
# after authentication login the user or set cookies or modify the session or some other action can be taken
return HttpResponse("Some response or use render for html page")
username and password will be coming from your post request.
If you want to extend default Django user model you can use django user model as onetoonefield in your extended model.
class AppUser(models.Model):
user = models.OneToOneField(User)
... # other custom fields
I have a model with a foreignkey to another model
class Person(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
class Organisation(models.Model):
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
contact = models.ForeignKey(Person)
I want to use a CreateView to be able to create a new Organisation, but be able to enter a new contact person details on the same page (i.e. when a new organisation is created, a new contact person must also be created).
What is the nicest (DRY) way to do this?
In the CreateView use the model that has the ForeignKey and since it inherits the FormMixin's form_class use the modelform_factory for that model with extra fields the fields of ForeignKey's model. Finally, overload either the validation or save methods with a get_or_create with the ForeignKey's model fields, passing the result to the ModelForm.
An alternative approach would be to chain two CreateViews. First with the Organization as the model, using the Contact's CreateView URL as its success_url. You can even use js to replace the first submit with the html of the second view.
Or you can try some hacks floating around utilizing formsets, though I prefer the first two methods in your case. The formset hacks are better suited for many-to-many relationships.
I'm only using forms.Form but I'm trying to show two choice fields that have selections of the associated models.
It basically needs to show the same names but in both fields. Here's what I'm using.
class ManagersForm(forms.Form):
class Meta:
model = A
leader = forms.ChoiceField()
co-leader = forms.ChoiceField()
Is there not just a way that I can parse the users?
users = MyUser.objects.filter(a=i)
You need to use a ModelForm not Form, if the field from the Model is a ForeignKey the form will render the field as a dropdown of the associated model:
class ManagersForm(forms.ModelForm):
class Meta:
model = A
I have a video model that stores 'likes' as a manytomany field with User.
e.g
class Video(models.Model):
...
likes = models.ManyToManyField(User)
....
When I create a ModelForm based on Video Likes is displayed as a dropdown with a list of all users. This is obviously ont what I want. I would like a particular user to be able to add/ remove their own name from this list. How do I instead display 'likes' as a checkbox and still have the form validate correctly?
In your model form, create a custom field for likes; when this field is checked, set likes to request.user.
from django import forms
class VideoForm(forms.ModelForm):
likes = forms.BooleanField(label='Mark as favorite?')
class Meta:
model = Video
This will render likes as a checkbox (the default widget for BooleanField).
I would write my own widget for this field (or replace the field with a boolean field): https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets