I am trying to make a django model to get user address including country and city.Please give a guidance to have a drop down list of countries and cities.
You can use django-cities-light (https://github.com/yourlabs/django-cities-light). It has a pre-populated database. But if you want to use your own database create a model of city and a model of country with city as foreign key. Then add city field as a foreign key in your address model.
class Address(models.Model):
city = models.ForeignKey('City')
class Country(models.Model):
name = models.CharField(max_length=50, unique=True)
class City(models.Model):
name = models.CharField(max_length=50)
country = models.ForeignKey('Country')
Related
I want to use UUIDs for database ids instead of autoincrement integers. I understand that this can be done by overriding the id in the Model class, for example:
from django.db import models
class Publication(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=30)
class Article(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
headline = models.CharField(max_length=100)
publications = models.ManyToManyField(Publication)
But, there is still a problem. The automatically generated table for the ManyToMany field uses an auto-incremented id, not a UUID.
Of course, this can address this by defining a "through" table as follows:
...
class Article(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
headline = models.CharField(max_length=100)
publications = models.ManyToManyField(Publication, through="ArticlePublication")
class ArticlePublication(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
publication = models.ForeignKey(Publication, on_delete=models.CASCADE)
But, according to the Django docs, when you use a "through" table: "Unlike normal many-to-many fields, you can’t use add(), create(), or set() to create relationships". Also, not "DRY".
I want to use add, create and set , and I like the "DRY" feature of the ManyToMany field. I also want to use UUIDs as ids.
I looked for a parameter to ManyToMany to pass in the definition of the automatically created "id" field, but there isn't one.
Am I missing something, or is this a limitation of Django's support for UUID as primary key?
I am using ASP.net MVC.
my question is that I have 2 tables patient and doctors and I want to create one common Userlogin table for both this table. So when I store username and password in UserLogin table it can determine whether it is Patient username or password or Doctor.
Can anyone please help me what should do? And give me some more idea about how and what I should change.
Doctors and patients are both subclasses of a more generic class, Users. You may want to research a topic called "Class Table Inheritance" There is a tag with that name here in Stackoverflow, and there are many articles on this topic on the web.
Class Table Inheritance is one way of making up for the fact that the relational model does not have a built in mechanism for inheritance.
Briefly, Your userlogin table needs a primary key, and username may not be suitable for this purpose. Call this key, UserId. Doctors and Patients do not need a unique id of their own. Instead, you can include UserId in both the doctor table and the patient table. This column should be the primary key of the doctor and patient table. In addition, this field should be named as a foreign key that references UserID in the UserLogin table.
The UserLogin table should probably include columns (fields) for all the features that are common to both doctors and patients, such as first name, last name, etc.
When you want to know if a user is a doctor or not, just join UserLogin and Doctor. Non doctors will drop out of the join.
Step-1. Create RoleMaster Table and add patient , doctors , receptionist as roleName in rolemaster table.
ColumnName Datatype
RoleId int
RoleName nvarchar(50)
Status bit
IsDeleted bit
CreatedDate datetime
step-2 Add RoleId As Reference in userLogin table and profileTable.
I have the following MySQL class.
class MyClassA(db.Model):
b = db.Column(db.String(12))
c = db.Column(db.String(12))
I want to make sure that the values of b are unique over the whole column. How can I change this declaration to enforce that constraint?
Well, you can use the flag "unique=True" like flask sql alchemy's documentation:
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String(50), **unique=True**)
email = Column(String(120), unique=True)
Flask SQL Alchemy
I hope this helps
I have Project, ProjectImage, and ProjectImageCategory.
a Project hasmany ProjectImage and a ProjectImage belongs to a Project
a ProjectImageCategory hasmany ProjectImage and a ProjectImage belongs to a ProjectImage
How should I name the model classes respectively the database tables and foreign keys, so that CakePHP would bind them.
Thanks
Project model use projects table and ProjectImageCategory use project_image_categories table and ProjectImage use project_images table, right?
the FK is ta name of the model with slug +_id , for example Project , to use a FK with this model you need set project_id , if project hasMany ProjectImage, in your project_images table you need a project_id column.
I know that normally Django would create a foreign key called user_id if I simply do something like
from django.db import models
class Order(models.Model):
user = models.ForeignKey(User)
comments = models.CharField(max_length=400)
date_created = models.DateTimeField('created date')
class User(models.Model):
name = models.CharField(max_length=200)
age = models.IntegerField()
but what if I need three distinct foreign key in Order that all points to User? The three foreign keys would be user_created, user_modified, and user_status.
The solution is actually straight forward:
class Order(models.Model):
user_status = models.ForeignKey(User, related_name='orders_status')
user_created = models.ForeignKey(User, related_name='orders_created')
user_modified = models.ForeignKey(User, related_name='orders_modified')
You just need to define separate related_names to avoid ambiguity when accessing the Order from the User object.