Sorry, I don't know what should be the title for this question. But here is my question.
In Django we User table comes as default.
Now I have another table User_Info which has User id as (one-to-one/foreign key),
I don't know what to take one-to-one or foreignkey.
Now I want to access this User_Info table which has enrollment through the User table in HTML. (Is it possible?)
example:-
class User_info(models.Model):
mobileNo = models.BigIntegerField(blank=True)
User_id = models.OneToOneField(User, on_delete=models.CASCADE)
we have User class as default and has primary key, username, email.
<h1> Name {{ user.username }} </h1> <!-- we can have username stored in -->
<h1> mobileNo {{ user.(i need help here, what to write)</h1> <!-- from user how to get mobile number-->
If I am missing anything else please ask.
Thank you:)
I forget to mention that the user has already been logged in so I can have the user table access in HTML.
Also 1 user has only 1 mobile number.
Rename your Model class to be according to Django coding style
class UserInfo(models.Model):
mobile_number = models.BigIntegerField(blank=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
One-to-one relationship docs
user.user_info.mobile_number
Related
I'm using the social_django package for the first time. The project only requires authentication for the application through Facebook so I implemented the SOCIAL_AUTH_FACEBOOK method into my app, which retrieves basic info like username, first name, last name, email address. I also get the user's Facebook profile picture through SOCIAL_AUTH_FACEBOOK_EXTRA_DATA.
I want to figure out how to refer to the usersocialauth model as a parent key in my model. Right now, it's associating everything to auth_user model, which doesn't allow me to access the Facebook profile picture in the template.
Here is what I am doing at the moment:
models.py
from social_django import models as oauth_models
class Review(models.Model):
...
author = models.ForeignKey(oauth_models.USER_MODEL,
on_delete=models.CASCADE)
I am able to access the basic fields in the template through <p>{{ review.author.name }}</p>, but to see the profile picture, I have to do something like this:
{% for author in backends.associated %}
<img src="{{ author.extra_data.picture.data.url }}" alt="">
{% endfor %}
I would like to simplify this and just refer to the usersocialauth model so I can access all the data from one place in the template.
Found an easier solution.
I can keep ForeignKey relationship with the regular user model and use author.social_auth.all.0.extra_data.picture.data.url in the template.
I am building a car rental website with Django. Currently working on user authentication.The user can either use his/her username/email and password to login.It's working fine. My question is, how will I change the label for username field to "username/email" so that the user can understand that either username or email can be entered. I cannot make changes in the login.html template because I have used there {{ form.as_p }} tag and the concerned portion in my forms.py file has:
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
I am not able to understand where to make changes. Please help.
You can set custom label for form field like this
class LoginForm(forms.Form):
username = forms.CharField(label='username/email')
password = forms.CharField(widget=forms.PasswordInput)
I'm working on a small Django website for a photographer.
There are a contact form and a booking. A percentage of these contact leads will be lost. If the customer books, the app should be able to follow up with the customer.
I've been thinking about how to proceed and my idea is to create a contact table with the information that the customer provides from the contact form and a second table that would have a "One to One" relationship in case that the lead becomes a customer.
In a later stage after the event has happened, there will be a private access folder where the user can download his pictures. And has to be a user in order to be able to log in and have authorization and authentication.
How would you design the database and the models in Django? Any advice or considerations in order to tackle de problem in the best way possible?
Thanks for your help!
models.py
class contact(models.Model):
user = models.OneToOneField(User)
Phone = models.CharField(default='', max_length=20)
event_type=models.CharField(default='',choices=create_a_list_of_choices_)
date=models.DateField(auto_now=True)
email=models.EmailField()
image = models.ImageField(blank=True,upload_to='users_photos',)
class Booking(models.Model):
User=models.ForeignKey(
User,
on_delete=models.CASCADE,
)
Extra=models.CharField(max_length=50,blank=True)
Deposit=models.CharField(max_length=50)
Time=models.DateField(auto_now=True)
Notes=models.TextField(max_length=250,blank=True)
Price=MoneyField(max_digits=14, decimal_places=2, default_currency='USD')
this is how your models should look like you can add some more other fields, and for the moneyFiled you can install it by following this link :https://github.com/django-money/django-money
How to create new titles for authors using inlineformset factory by selecting authors created by logged in users only.
Models.Py
Model1
user=models.ForeignKey(settings.AUTH_USER_MODEL)
Author = models.CharField(max_length=100)
Model2
Author_for_title=models.ForeignKey(Model1)
title=models.CharField(max_length=100)
Now i want to have a form where users can select the Author and type title in input field to update.
So far everything is fine. But only thing i am not able to solve is that:
How to use queryset so that Author select field display authors
created by logged in Users only?
formset=inlineformset_factory(Model1,Model2,,can_delete=True,fields=('__all__',),extra=3)
I found no answer anywhere, please help with an example.
Got the answer, thanks to documentation.
in views.py
new_formset = inlineformset_factory(Model1,Model2,can_delete=False,exclude=('user',),extra=3)
for forms in new_formset:
forms.fields['Author_for_title'].queryset = Model1.objects.filter(user=request.user)
I have two models like below
class Author(models.Model):
name = models.CharField(max_length=256)
city = models.CharField(max_length=256)
class Book(models.Model):
author = models.ForeignKey(Author)
name = models.CharField(max_length=256)
price = models.FloatField(max_length=256)
So i am displaying the list of author and a link to edit the author record like below
{% for author in authors %}
<p></p>
{% endfor %}
So if we want to edit only single Author class, then django UpdateView is simply enough to do so, but when a user try to edit an author, all the foreignkey records (Book records) related to this model should also be in Edit mode. I mean the clicked Author, and all the Book instances related to this Author with ForeignKey should also be in the edit mode exactly same in the Django Admin
I know that we can use django inline_formsets would be the choice, but i am really confused on how to implement it on my site(front end). So can anyone please let me know how to edit the author record along with its child records in a single page on our site ?