I have two models with a unique key: EMAIL. For both of the models the field EMAIL is unique, but some of the emails of model A do not necessarily exist in model B. I would like to perform an inner join, but since I'm quite new to Django I have no idea how to do this.
You should define the Email model look like this
class Email(models.Model):
"""
Email model.
"""
email = models.EmailField(
unique=True,
error_messages={
'unique': "This email has already been registered."
}
)
def __str__(self):
return self.email
Then create 2 models related to Email model by using ForeignKey.
Or you just need to save email by using EmailField for each model.
class ModelA(models.Model):
"""
A model.
"""
email = models.EmailField(
verbose_name="Email",
unique=True,
)
...other fields
In Django, you need to make a relation between table in order to apply any type of join
class ModelA(models.Model):
email = models.EmailField(unique=True)
model_b = models.ForeignKey(to=ModelB, null=True, blank=True)
class ModelB(models.Model):
email = models.EmailField(unique=True)
then
data = ModelA.objects.filter(model_b__isnull=False)
Instance with same email id should be linked with each other.
Related
There are many questions about building model forms from multiple tables, but I couldn't find a solution to my problem.
Background:
In model.py I have 3 simple models with 1-many relationship
class site(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
sitename = models.CharField(max_length=150)
site_address = models.CharField(verbose_name='Address', max_length=200, null=True, blank=True)
class block(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
blockname = models.CharField(max_length=150)
sitename = models.ForeignKey(site, on_delete=models.CASCADE, )
class quarter(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
quartername = models.CharField(max_length=150)
blockname = models.ForeignKey(block, on_delete=models.CASCADE, )
I have separate forms for each model - where user can add, edit and delete and site, block using standard djando modelform and modelformset.
The challenge I'm facing is with creating a quarter form. On a quarter from I want user to select a site from drop-down, which will populate block and then use can enter quarter name and save submit.
Now, 1 possible solution is to include site as FK in quarter and that will solve the problem - but I don't want to do that. I even tried create separate form instances and call it in view and template - but the problem with this solution is that the mandatory field name from site are also displayed.
How can I show the sitename dropdown on quarter form - where on selecting sitename the user is shown corresponding blockname. On click of save button the data for quarter is saved in db (don't have to save site or block data because we already have those entries)
Any suggestion on approach or solution?
I have used function based View to get data from post Request. I need to get many data that also include a primary key field too. I need to push data into two models. While doing so, I encountered error.
#accounts models.py
from django.db import models
class User(models.Model):
user_id = models.CharField(max_length=50,)
name = models.CharField(max_length=200)
phone = models.CharField(max_length=200)
email = models.EmailField(max_length=254)
#app models.py
from accounts.models import User #from accounts models.py
class job(models.Model):
user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
......
#views.py
data = job(.....,map_link=map_link,user_id=user_id)
data.save()
info=User(name=name,email=email,phone=phone,user_id=user_id)
info.save()
Error Message: http://dpaste.com/03Z0EPB
enter image description here
I have two models with foreign key.
class Ad(models.Model):
town = models.CharField(max_length=30)
owner = models.ForeignKey(SpaUser, on_delete=models.CASCADE) #My foreign key
contact = SpaUser.objects.filter(email= 'owner').values('firstname ') # I want to access SpaUser.firstname to display in django admin
class SpaUser(models.Model):
email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
firstname = models.CharField(max_length=30, blank=True)
lastname = models.CharField(max_length=30, blank=True)
I use Ad.contact in Ad model to acess SpaUser.firstname in Spauser model but i can't.
Help me please
You can make contact a property instead (and you should make use of the foreign key owner instead of filtering from another queryset of SpaUser):
class Ad(models.Model):
#property
def contact(self):
return self.owner.firstname
hello i have a model named Assigned_problem in which a need to add two foreign keys of the model user in it. one foreign key represents the person who assigned the problem and the other foreign key represents the person to whom the problem is assigned. so in the course of updating the modifications done one the database structure i ave the following errors
mini_url.Assigned_problem.assigned_to: (fields.E305) Reverse query name for 'Assigned_problem.assigned_to' clashes with reverse query name for 'Assigned_problem .assigned_by'. HINT: Add or change a related_name argument to the definition for 'Assigned_problem.assigned_to' or 'Assigned_problem.assigned_by'.
THIS IS MY MODEL STRUCTURE
class Assigned_problem(models.Model):
assignation_status = models.IntegerField()
assigned_date = models.DateTimeField(auto_now_add=True, auto_now=False,
verbose_name="Assigned Date")
assigned_to = models.ForeignKey(User)
assigned_by = models.OneToOneField(User)
problem = models.OneToOneField('Problem')
def __str__(self):
return "{0}".'format'(self.problem.problem_content)
With
assigned_to = models.ForeignKey(User)
assigned_by = models.OneToOneField(User)
you declared two ForeignKeys to the model User. They're not distinguishable by their relation to User itself. Try to add a related_name attr for each of your foreign keys to User.
assigned_to = models.ForeignKey(User, related_name="User assigned to")
assigned_by = models.OneToOneField(User, related_name="User assigned by")
See the django docs
Using the standard Django example model for M2M relationships. I would like to return a list of Person (people) that don't have a Membership (ie. no related records in the Membership table. How would I go about doing this? Is anybody able to point me in the right direction?
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
Thank you.
Person.objects.filter(membership__isnull=True)
https://docs.djangoproject.com/en/1.4/ref/models/querysets/#isnull