get specific field data from another table in django - python

i have a table in models.py with class name registration and another table profileimage with foreignkey relation with registration model now i want to
get verify the email address by using profileimage table
models.py
class Registration(models.Model):
YourName = models.CharField(max_length=30)
Email = models.EmailField(max_length=254,verbose_name='email')
password = models.CharField(max_length=254,verbose_name='password',null=True)
PhoneNumber = models.IntegerField(null=True,verbose_name='email')
CompanyName = models.CharField(max_length=30)
WebSiteName = models.CharField(max_length=30)
NumberOfEmplyes = models.IntegerField(null=True)
YourRoleInCompany = models.CharField(max_length=100)
SoftwareUsedBefore = models.CharField(max_length=30)
Turnout2017 = models.IntegerField(null=True)
Turnover2016 = models.IntegerField(null=True)
Description = models.CharField(max_length=30)
ClientArgument = models.CharField(max_length=2000,null=True)
PaymentsPlan = models.CharField(max_length=100,null=True)
class ProfileImages(models.Model):
MemeberName = models.OneToOneField('Registration',on_delete=models.CASCADE,blank=True)
profile_image = models.ImageField(upload_to='media/profile_image',default=True,null=True)
def __str__(self):
return "user profile image"
now for example i have email adress which i get from sessions now e.g example#stack.com now i want to use this email to fillter record in this way.
profile_image=
ProfileImages.objects.filter(Registration__Email=email).select_related()
let me know what is the exact way to perform this query

ProfileImages.objects.get(MemeberName__email=email)

I think I would do it as follows
class ProfileImages(models.Model):
profile_image = models.ImageField(upload_to='profile_image',blank=True,null=True)
...
And in the Registration
class Registration(models.Model):
YourName = models.CharField(max_length=30)
Email = models.EmailField(max_length=254,verbose_name='email')
....
image_profile = models.ForeignKey(
ProfileImages, verbose_name='Profile Image', on_delete=models.CASCADE, blank=True, null=True)
Notice, that 'media' word in upload_to it is better to define it in settings.py

Related

Django Query Foreign Key and Many to many field

Struggling with Django query, need some help.
So I would like to get data for 5 different variables, accounts_data = (only username), user_account_data = (name), country_data = (iso_code), category_data = (choice), permisisons_data = (permissions).
All data of the models is connected with UserAccount table, so need to get data from other tables only for existing user_accounts
Example of models:
class Account(models.Model):
username = models.CharField(max_length=30)
password = models.CharField(max_length=50)
status = models.CharField(max_length=60)
class UserAccount(models.Model):
name= models.CharField(max_length=100)
surname = models.CharField(max_length=100)
category= models.ManyToManyField(Category)
account = models.ForeignKey(Account)
country = models.ForeignKey(Country)
permissions = models.ForeignKey(Permissions)
class Country(models.Model):
iso_code = models.CharField(max_length=6)
zip_code = models.CharField(max_length=10)
class Category(models.Model):
choice = models.CharField(max_length=12)
connected_category = models.CharField(max_length=12)
class Permissions(models.Model):
permission = models.CharField(max_length=50)
restriction = models.CharField(max_length=30)

Django Multiple Types of Users, one base usertype?

I am attempting to create a food delivery app clone. Which has two types of users Restaurant User and Customers. I want the restaurant user to be able to also be customers, using the same login info.
This is how my models are setup right now:
class Restaurant(models.Model):
user_name = models.OneToOneField(User, on_delete=models.CASCADE, related_name='restaurant')
full_name = models.CharField(max_length=500)
phone = models.CharField(max_length=500)
employee_id = models.CharField(max_length=500)
class Customer(models.Model):
user_name = models.OneToOneField(User, on_delete=models.CASCADE, related_name='customer')
full_name = models.CharField(max_length=500)
email = models.CharField(max_length=500)
avatar = models.CharField(max_length=500)
phone = models.CharField(max_length=500)
address = models.CharField(max_length=500)
I don't believe this works as intended. Would it be better to create a base usertype, for login and shared information, like user_name, full_name, phone?
I created the following for that purpose:
class CustomUser(AbstractUser):
user_name = models.OneToOneField(User, on_delete=models.CASCADE, related_name='CustomUser')
full_name = models.CharField(max_length=500)
phone = models.CharField(max_length=30)
But I'm not sure how to connect this between the other user models. Or if this is even the proper way to do it.
So how can I achieve what I am trying to do, what would my models look like?
This should be helpful https://docs.djangoproject.com/en/3.1/topics/db/models/#model-inheritance
You can use the custom user as the base class. So you could pass that custom model as such:
class Restaurant(CustomUser):
user_name = models.OneToOneField(User, on_delete=models.CASCADE, related_name='restaurant')
full_name = models.CharField(max_length=500)
phone = models.CharField(max_length=500)
employee_id = models.CharField(max_length=500)
class Customer(CustomUser):
user_name = models.OneToOneField(User, on_delete=models.CASCADE, related_name='customer')
full_name = models.CharField(max_length=500)
email = models.CharField(max_length=500)
avatar = models.CharField(max_length=500)
phone = models.CharField(max_length=500)
address = models.CharField(max_length=500)
Also, you should declare the CustomUser as abstract.
class CustomUser(AbstractUser):
user_name = models.OneToOneField(User, on_delete=models.CASCADE,related_name='CustomUser')
full_name = models.CharField(max_length=500)
phone = models.CharField(max_length=30)
class Meta:
abstract = True
Follow this:
class Restaurant(models.Model):
owner = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='restaurant')
name = models.CharField(max_length=500)
address = models.TextField(max_length=500)
phone = models.CharField(max_length=500)
class RestaurantStaff(models.Model):
restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE, related_name='staff')
staff = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='staff')
employee_id = models.CharField(max_length=500)
class CustomUser(AbstractUser):
full_name = models.CharField(max_length=500)
email = models.EmailField(unique=True)
avatar = models.CharField(max_length=500)
phone = models.CharField(max_length=500)
address = models.CharField(max_length=500)
Note: Never duplicate the columns, if you inherit the same model in two other model, both of those model will have same fields/columns of the inherited model.
Here is the explaination of model structure:
There is only one CustomUser model that holds the data for all users(customer, restaurant owner, staff)
Restaurant model holds the data related to restaurant(its owner link, name, address, restaurant phone number)
RestaurantStaff model hold the relation of a restaurant and a user(which is the restaurant staff), so there you can add more fields like, "start_date", "termination_date", "post"
Your choice of field types are incorrect(example phone number should be interger, but you choose character, email should be email field, but you choose character). Use what django already provide as it helps in validation and serialization in API.

How to fetch data from database django

I have created a CustomerRegistration model and I want that if the phone no exists in the database, then all the details of the customer will display automatically in form.
here is my models.py
class CustomerRegistration(models.Model):
name = models.CharField(max_length=254, null=False)
email = models.EmailField(max_length=254, null=False)
date_of_birth = models.DateTimeField(null=False)
country_id = models.ForeignKey('accounts.Country', null=False, on_delete=models.CASCADE, related_name='Country')
state_id = models.ForeignKey('accounts.State', null=False, on_delete=models.CASCADE, related_name='State')
cities_id = models.ForeignKey('accounts.City', null=False, on_delete=models.CASCADE, related_name='city')
address = models.CharField(max_length=254, null=False)
refernce_by_person_name = models.CharField(max_length=254, null=False)
refernce_by_person_contact_no = models.IntegerField(null=True)
phone_no = models.IntegerField(null=False, primary_key=True)
alternate_no = models.IntegerField(null=False)
hobbies = models.CharField(max_length=254)
def __str__(self):
return self.name
Have a look at making queries in the django documentation. You will want to make a query to your databse, and if you get any results, populate your form with the correct info.
This would look something like:
from .models import CustomerRegistration
results = CustomerRegistration.objects.all().filter(phone=given_number)
then, if there are any results, you can use it to populate your form!
You can query like this to understand that user has phone number or not or something like that:
CustomerRegistration.objects.filter(phone_no__isnull=False)
isnull specifies that a field is none or not

How to link address model to views

I'm trying to create an address form with multiple address, where the user can choose home or shipping address. I have the current model:
from django.db import models
from django.contrib.auth.models import User
from PIL import Image
class Address(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60, default="Miami")
state = models.CharField(max_length=30, default="Florida")
zipcode = models.CharField(max_length=5, default="33165")
country = models.CharField(max_length=50)
class Meta:
verbose_name = 'Address'
verbose_name_plural = 'Address'
def __str__(self):
return self.name
So I was wondering if that's correct.
Anyway, I was wondering how with the current model I can create a view so I can have the address form. Using a normal model would be "easy" but how can I do it using the through option in the model?
Could someone lend me a hand please?
Thank you
use a foreign key to point to your address model:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
nick_name = models.CharField('Nick name', max_length=30, blank=True, default='')
bio = models.TextField(max_length=500, blank=True)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
addresses = models.ForeignKey(Address) # <-- fix here
Hope this helps!
You should declare ForeignKey with '<app>.<model>' format:
class AddressType(models.Model):
address = models.ForeignKey('yourapp.Address', on_delete=models.CASCADE)
profile = models.ForeignKey('yourapp.Profile', on_delete=models.CASCADE)
or directly give the class:
address = models.ForeignKey(Address, on_delete=models.CASCADE)
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
Both of the other answers were incorrect, I ended up modifying everything and also creating a new model, here it is:
class Address(models.Model):
name = models.CharField(max_length=100, blank=False)
address1 = models.CharField("Address lines 1", max_length=128)
address2 = models.CharField("Address lines 2", max_length=128, blank=True)
city = models.CharField("City", max_length=64)
# state = USStateField("State", default='FL')
state = models.CharField("State", max_length=128, default='FL')
zipcode = models.CharField("Zipcode", max_length=5)
user = models.ForeignKey(Profile, on_delete=models.CASCADE, blank=False)
class Meta:
verbose_name_plural = 'Address'
def __str__(self):
return self.name

How work with two models and one form?

I've got two models ('Student', 'Instructor') and one form 'RegisterForm' with two selectors (for types). How can i save data from form in corresponding model if in class Meta field 'model' just for the only one model?
I guess, my way is wrong, but so i asking.
class Student(models.Model):
username = models.CharField(max_length=30)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
password = models.CharField(max_length=30)
email = models.EmailField()
passed_tests = models.IntegerField(default=0)
available_tests = models.IntegerField(default=0)
def __str__(self):
return self.username
class Instructor(models.Model):
username = models.CharField(max_length=30)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
password = models.CharField(max_length=30)
email = models.EmailField()
written_tests = models.IntegerField(default=0)
def __str__(self):
return self.username
I think in your case You can use relationships. For example try ForeignKey(relation one to many) od ManyToMany(relation many to many).

Categories