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).
Related
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)
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.
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
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
I have a classic model containing some contact data (mail, addr, names, ...), and an other which is supposed to link to three contacts (an admin, an owner, a tech). A contact can be registered as admin and as tech for example.
class Contact(django.db.models.Model):
user = models.OneToOneField(User)
city = models.CharField(max_length=255, blank=True)
country = models.CharField(max_length=2, blank=True)
email = models.EmailField(max_length=255, blank=True)
first_name = models.CharField(max_length=255, blank=True)
family_name = models.CharField(max_length=255, blank=True)
phone = models.CharField(max_length=255, blank=True)
class Product(... Some parents)
name = models.CharField(max_length=255)
contacts ?
I don't know how to link 3 contacts to my other Model ... and generate a form with a queryset already existing.
Some advices ?
Thanks
just use foreign keys
example:
class Product(... Some parents)
name = models.CharField(max_length=255)
admin = models.ForeignKey(Contact, related_name="admins")
owner = models.ForeignKey(Contact, related_name="owners")
tech = models.ForeignKey(Contact, related_name="techs")
if you want to generate the form use a modelform
class MyForm(forms.ModelForm):
class Meta:
model=Product
the select widgets will be filled automatically
EDIT: to display your contact in a human friendly way use
class Contact(django.db.models.Model):
<usual stuff>
def __unicode__(self):
return u"%s" % self.firstname # or any field you wish
see https://docs.djangoproject.com/en/1.10/ref/models/instances/#str
if you use python 3 replace unicode by str