Hey guys, quick question. This is my one-to-many relational database in Django. What is the Django syntax to pull everything that has a category of “Florida”? I tried the following syntax:
class category(models.Model):
name = models.CharField(max_length=20, blank=True)
def _str_(self):
return self.name
class contact(models.Model):
# Creates a model named Owner, via class
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
phone_number = models.CharField(max_length=10, validators=[REGEX_PHONE_NUMBER])
phone_number_email = models.CharField(max_length=300, blank=True)
category = models.ForeignKey(category, on_delete = models.CASCADE)
def _str_(self):
return self.name
category.objects.get(name = "Florida").contact_set.all()
But got this error, in the Django shell:
MultipleObjectsReturned: get() returned more than one category -- it returned 2!
I tried researching online, however, my syntax looks to be incorrect. Any thoughts?
See QuerySet API reference | Django documentation | Django
Have you worked your way through either the Official Django Tutorial or the Django Girls tutorial? If you haven’t, either of them are going to provide you with a lot of foundational knowledge you will need when working with Django.
@KenWhitesell Yes, I have reviewed that doc. I also tried using filter
, and that returned no data, however, I triple checked that database has data.
What does your filter
version of your query look like?
And, related to that, what type of object does a filter
return?
contact.objects.filter(category__name="dog")
<QuerySet []>
Filter is a QuerySet, just not sure why it is returning an empty QuerySet. I tried with many different values in my category
table
This is my table for category, dog
is there
Ok, but the question is, do you have a Contact object that is referring to category.id == 13?
I think that is where my error lies. My contact table index number does not match up 1-for-1 with my category table index number. I thought it did not have to match though?
My category table is above, and this is my contact table:
Contact has a column named category
. It’s that ID that you’re using to find references in the Category model.
@KenWhitesell thank you for taking time out of your day to direct me towards the source, versus providing me with an answer. I appreciate it, however, I am not understanding your last question, or statement.
What is it about my statement that is confusing you?
Your foreign key to the Category model is the field named category
in Contact.
was a question or a statement?
Did you mean to say " Contact has a column named category
. Is that the ID that you’re using to find references in the Category model?"
It wasn’t a question, it was a statement.
Yes, that is how I have it.
class contact(models.Model):
# Creates a model named Owner, via class
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
phone_number = models.CharField(max_length=10, validators=[REGEX_PHONE_NUMBER])
phone_number_email = models.CharField(max_length=300, blank=True)
category = models.ForeignKey(category, on_delete = models.CASCADE)
I have category as the foreign key at the contact class