ordinarily if I were writing a sql statement to this I would do something like this,
SELECT * FROM (django_baseaccount LEFT JOIN django_account ON django_baseaccount.user_id = django_account.baseaccount_ptr_id)
LEFT JOIN django_address ON django_account.baseaccount_ptr_id = django_address.user_id;name
how do I put this into the Djagno way of querying the database using the API, i.e.
TradeDownloads.objects.filter(online=1)[:6]
My models
BASE ACCOUNT
class BaseAccount(models.Model):
user = models.ForeignKey(User, unique=True)
def __unicode__(self):
"""
Return the unicode representation of this customer, which is the user's
full name, if set, otherwise, the user's username
"""
fn = self.user.get_full_name()
if fn:
return fn
return self.user.username
def user_name(self):
"""
Returns the full name of the related user object
"""
return self.user.get_full_name()
def email(self):
"""
Return the email address of the related user object
"""
return self.user.email
ACCOUNT
class Account(BaseAccount):
"""
The account is an extension of the Django user and serves as the profile
object in user.get_profile() for shop purchases and sessions
"""
telephone = models.CharField(max_length=32)
default_address = models.ForeignKey(Address, related_name='billing_account', blank=True, null=True)
security_question = models.ForeignKey(SecurityQuestion)
security_answer = models.CharField(max_length=200)
how_heard = models.CharField("How did you hear about us?", max_length=100)
feedback = models.TextField(blank=True)
opt_in = models.BooleanField("Subscribe to mailing list", help_text="Please tick here if you would like to receive updates from %s" % Site.objects.get_current().name)
temporary = models.BooleanField()
def has_placed_orders(self):
"""
Returns True if the user has placed at least one order, False otherwise
"""
return self.order_set.count() > 0
def get_last_order(self):
"""
Returns the latest order that this customer has placed. If no orders
have been placed, then None is returned
"""
try:
return self.order_set.all().order_by('-date')[0]
except IndexError:
return None
def get_currency(self):
"""
Get the currency for this customer. If global currencies are enabled
(settings.ENABLE_GLOBAL_CURRENCIES) then this function will return
the currency related to their default address, otherwise, it returns
the site default
"""
if settings.ENABLE_GLOBAL_CURRENCIES:
return self.default_address.country.currency
return Currency.get_default_currency()
currency = property(get_currency)
def get_gateway_currency(self):
"""
Get the currency that an order will be put through protx with. If protx
currencies are enabled (settings.ENABLE_PROTX_CURRENCIES), then the
currency will be the same returned by get_currency, otherwise, the
site default is used
"""
if settings.ENABLE_PROTX_CURRENCIES and settings.ENABLE_GLOBAL_CURRENCIES:
return self.currency
return Currency.get_default_currency()
gateway_currency = property(get_gateway_currency)
ADDRESS
class Address(models.Model):
"""
This class encapsulates the data required for postage and payment mechanisms
across the site. Each address is associated with a single store account
"""
trade_user = models.BooleanField("Are you a stockist of N Products", help_text="Please here if you are a Stockist")
company_name = models.CharField(max_length=32, blank=True)
line1 = models.CharField(max_length=200)
line2 = models.CharField(max_length=200, blank=True)
line3 = models.CharField(max_length=200, blank=True)
city = models.CharField(max_length=32)
county = models.CharField(max_length=32)
postcode = models.CharField(max_length=12)
country = models.ForeignKey(Country)
account = models.ForeignKey('Account')
class Meta:
"""
Django meta options
verbose_name_plural = "Addresses"
"""
verbose_name_plural = "Addresses"
def __unicode__(self):
"""
The unicode representation of this address, the postcode plus the county
"""
return ', '.join((self.postcode, str(self.county)))
def line_list(self):
"""
Return a list of all of this objects address lines that are not blank,
in the natural order that you'd expect to see them. This is useful for
outputting to a template with the aid of python String.join()
"""
return [val for val in (self.line1, self.line2, self.line3, self.city, self.county, self.postcode, self.country.name) if val]
"ordinarily if I were writing a sql statement"
Welcome to ORM. You're not writing SQL so remove this from the question. Do not ever post SQL and ask how to translate SQL into ORM. Translating SQL limits your ability to learn. Stop doing it.
Write down what the result is supposed to be.
It appears that you are getting all Account objects. Period.
At some point in a view function or template you want to get an Address, also.
for a in Account.objects.all():
a.default_address # this is the address that SQL brought in via a "join".
That's it. Please actually do all the examples in the Django tutorial. Actually type the code from the examples and see how it works.
All "join" operations are SQL workarounds. They're a weird SQL-ism, and have nothing to do with the underlying objects. So stop using SQL terminology to describe what you want.
Django provides a clean way to fall-back to native SQL for complex queries see the official documentation: Performing raw SQL queries
Forget the SQL. What do you want to achieve from this query? What do you want to do with the results?
You haven't posted your models. Do they have the foreign keys defined? Can you just do a simple query and use select_related() to get the joined objects?
Edited to add What was wrong with the answer given the previous time you asked this question?
Edited again but everyone has shown you how to get the item via the foreign key! Forget the id, you don't need it. If you have an Account object a, you just do a.default_address to get the actual Address object that is related. If that doesn't work, then you're not posting the right models, as that will definitely work with the models you have posted.
Related
I am implementing a friend system similar to facebook where one can send friendship requests and accept requests and then see their friends. On the page where one sees the friend requests I am trying to filter out the users who have accepted the friend request and are friend already now. In the code below, 'u' is the current logged in user. Friendship table hold two fields , both foreign keys, please see below:
try:
already_friends = Friendship.objects.get(Q(from_friend=u) | Q(to_friend=u))
for x in already_friends.iterator():
my_requests = FriendRequest.objects.filter(Q(receiver=request.user) & ~Q(sender=x))
except ObjectDoesNotExist:
class FriendRequest(models.Model):
sender = models.ForeignKey(User, related_name='the_sender',on_delete=models.CASCADE)
receiver = models.ForeignKey(User, related_name='the_receiver', on_delete=models.CASCADE)
def __str__(self):
return "request sent"
class Meta:
unique_together = (('sender', 'receiver'),)
class Friendship(models.Model):
from_friend = models.ForeignKey(User, related_name="from_friend", on_delete=models.CASCADE)
to_friend= models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return "Friend request accepted"
class Meta:
unique_together = (('from_friend', 'to_friend'),)
When I use the query I wrote at the top using Q (complex queries) and then use iterator, I get the following error:
'Friendship' object has no attribute 'iterator'
How can I achieve what I intend to do using django models/queries?
You should use .filter(…) [Django-doc] not .get(…) [Django-doc]: .get(…) retrieves a single Friendship object, and will raise an error if there is no such object, or if there are multiple ones. .filter(…) on the other hand will return a (possibly empty) queryset of all Friendship records that satisfy the given predicate:
already_friends = Friendship.objects.filter(Q(from_friend=u) | Q(to_friend=u))
for x in already_friends.iterator():
my_requests = FriendRequest.objects.filter(Q(receiver=request.user) & ~Q(sender=x))
Furthermore it makes no sense to use Q(sender=x): x is a Friendship object, not a user object. You probably want to check if the from_friend_id or to_friend_id is the u, and thus use the other one to filter. Something like:
already_friends = Friendship.objects.filter(Q(from_friend=u) | Q(to_friend=u))
for x in already_friends.iterator():
if x.from_user_id = u.id:
my_requests = FriendRequest.objects.filter(receiver=request.user, sender_id=x.to_user_id)
else:
my_requests = FriendRequest.objects.filter(receiver=request.user, sender_id=x.from_user_id)
I am just starting Django Website;
I have deciced to go with a model (with SQLite DB), which have the following properties:
class Flow(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE,
verbose_name="Owner", default="ADMIN")
source = models.CharField(default='HTTP', choices=SOURCE_CHOICES, editable=True, max_length=12)
name = models.CharField(max_length=50, default=" ")
date = models.DateTimeField(verbose_name="Creation date")
I want to add others fields to this model depending on the source field value.
For example if the source field : 'File' is selected. I will create additionnal field (like file name, file directory ...) If 'Http' is selected, I will create a field URL.
Thus depending on the source field, I will have differents field and type.
I have read this kind of model is difficult to reprensent in Django; I am open minded to other kind of solution. My idea was to created as many model as the possible source value.
Even if you can create dynamic field in Django, you can't create dynamic column in Sqlite table FLOW.
If you plan to use same types of fields in different cases, you can create field with abstract name, for example path. That can be as URL as local file path.
In common way you need to create all columns for all choices for DB table.
class Flow(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE,
verbose_name="Owner", default="ADMIN")
source = models.CharField(default='HTTP', choices=SOURCE_CHOICES, editable=True, max_length=12)
name = models.CharField(max_length=50, default=" ")
date = models.DateTimeField(verbose_name="Creation date")
local_path = models.ImageField(upload_to='files', null=True, blank=True) # when source is file
url = models.URLField(null=True, blank=True) # when source is http
So local_path may be empty when you using HTTP source.
Inside view you can dynamically get (or set) value through serializer:
class FlowSerializer(serializers.ModelSerializer):
path = serializers.SerializerMethodField(method_name='get_path_value')
class Meta:
model = Flow
fields = ('owner', 'source', 'name', 'date', 'path')
def get_path_value(self, instance):
if instance.source == 'HTTP':
return instance.url
else:
return instance.local_path
So path will be different for different sources.
And maybe you will be need to install django rest framework for this solution.
EDIT1:
answering to question
So if I understand well, the best pratices should be to create 'blank'
columns
You definitely must to describe all columns in table (unless you using non Sql-like DB, such as MongoDB). So yes, create 'blank' columns, is only one possible way.
But you can override save method in model, for dynamically save fields:
class Flow(models.Model):
temp_path = None
path = models...
url = models...
choice = models...
def save(self, *args, **kwargs):
if choice == 'HTTP':
self.url = temp_path
else:
self.path = temp_path
super().save(*args, **kwargs)
Code above is just a quick idea. Not really working code.
You could implement a custom model field type for that, it helps you save a union object in one column in database. The raw type in database could be a JSON string or other serialized types, for the model field's usage, it's just a python native object!
Here is a piece of sample code: the main jobs are these two methods:
class HandField(models.Field):
# ...
def from_db_value(self, value, expression, connection):
if value is None:
return value
return parse_hand(value)
def to_python(self, value):
if isinstance(value, Hand):
return value
if value is None:
return value
return parse_hand(value)
Check the official docs for details.
Context: I'm forcing my self to learn django, I already wrote a small php based website, so I'm basically porting over the pages and functions to learn how django works.
I have 2 models
from django.db import models
class Site(models.Model):
name = models.CharField(max_length=50, unique=True)
def __str__(self):
return self.name
class Combo(models.Model):
username = models.CharField(max_length=50)
password = models.CharField(max_length=50)
dead = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
siteID = models.ForeignKey(Site, on_delete=models.PROTECT)
class Meta:
unique_together = ('username','siteID')
def __str__(self):
return f"{self.username}:{self.password}#{self.siteID.name}"
When creating a view, I want to get the Combo objects, but I want to sort them first by site name, then username.
I tried to create the view, but get errors about what fields I can order by Cannot resolve keyword 'Site' into field. Choices are: dead, id, password, siteID, siteID_id, timestamp, username
def current(request):
current = Combo.objects.filter(dead=False).order_by('Site__name','username')
return render(request, 'passwords/current.html',{'current':current})
Since I'm not necissarily entering the sites into the database in alphabetical order, ordering by siteID wouldn't be useful. Looking for some help to figure out how to return back the list of Combo objects ordered by the Site name object then the username.
You can order this by siteID__name:
def current(request):
current = Combo.objects.filter(dead=False).order_by('siteID__name','username')
return render(request, 'passwords/current.html',{'current':current})
since that is the name of the ForeignKey. But that being said, normally ForeignKeys are not given names that end with an ID, since Django already adds an _id suffix at the end for the database field.
Normally one uses:
class Combo(models.Model):
# …
site = models.ForeignKey(Site, on_delete=models.PROTECT)
if you want to give the database column a different name, you can specify that with the db_column=… parameter [Django-doc]:
class Combo(models.Model):
# …
site = models.ForeignKey(
Site,
on_delete=models.PROTECT,
db_column='siteID'
)
I have searched high and low and even in between and for some reason cannot come up with a clear answer...
I am using django1.9 and created this model:
class paymentInfo(models.Model):
"""
Model for storing payment info
- Username as ForeignKey from userToCard
- Store payment token
- Store last 4
- Store card/bank name
- Store bool value for Default method
"""
username = models.ForeignKey(User, db_column='username', on_delete=models.CASCADE)
token = models.CharField(max_length=10)
last_4 = models.IntegerField()
bank_name = models.CharField(max_length=50)
default = models.BooleanField(default=0)
class Meta: # meta class to define the table name
db_table = 'payment_methods'
verbose_name_plural = 'Payment Methods' # for the admin site display
ordering = ('username',)
def __str__(self):
# in __str__ you should return a value of type string
# so self.username changed to self.username.username
return self.username.username # value displayed in admin view
I have created some objects using some different usernames and want to filter out the paymentInfo objects by user.
When I store the object, the database stores the user pk under the username column instead of the actual username string. I am not sure why, but that is not my issue here.
My issue is when I am trying to filter out paymentInfo.objects using the username or the user pk. I cannot seem to filter it out and the error I normally get is thus: FieldError: Cannot resolve keyword 'username' into field. Choices are: bank_name, default, id, last_4, token
P.S. I am using MySQL
If I understood you right, you are trying to filter data by username from table User what is a foreign key. In this case, this should help
paymentInfo.objects.filter(username__name='John')
Thanks to the answers provided, I was able to work out a solution (mainly using #Aamir Adnan 's method.)
class paymentInfo(models.Model):
"""
Model for storing payment info
- Username as ForeignKey from userToCard
- Store payment token
- Store last 4
- Store card/bank name
- Store bool value for Default method
"""
user = models.ForeignKey(User, on_delete=models.CASCADE)
token = models.CharField(max_length=10)
last_4 = models.IntegerField()
bank_name = models.CharField(max_length=50)
default = models.BooleanField(default=0)
class Meta: # meta class to define the table name
db_table = 'payment_methods'
verbose_name_plural = 'Payment Methods' # for the admin site display
ordering = ('user',)
def __str__(self):
return self.user # value displayed in admin view
def __unicode__(self):
return '%s' % (self.user)
The new __unicode__ inside of my class was so that I did not receive this error anymore:
TypeError: coercing to Unicode: need string or buffer, User found
I'm still a novice so any help is gladly appreciated. Running Django 1.10
I'm trying to retrieve all the profiles that are assigned a particular manager but my query set always comes up empty.
Model.py
Blockquote
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=30, blank=False)
last_name = models.CharField(max_length=30, blank=False)
email = models.EmailField( blank=True, help_text='Optional',)
receive_email_notifications = models.BooleanField(default=False)
manager = models.ForeignKey(User, unique=False, blank=True, related_name='+', null=True)
def get_full_name(self):
"""
Returns the first_name plus the last_name, with a space in between.
"""
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def publish(self):
return self.save
def __str__(self):
return str(self.user)
View.py
Blockquote
def instrument_list(request):
# pulls all instruments from instrument model
instruments = Instrument.objects.all().order_by('instrument_name')
test = Profile.objects.filter(manager='jenn')
print(test)
# pulls all checklists from checklist model
checklists = Checklist.objects.all().order_by('created_date')
# takes instruments, pushes them to template with variable 'instruments'
return render(request, 'blog/instrument_list.html', {'instruments': instruments, 'checklists': checklists})
I've also tried filtering a single profile entry (with a non-foreign key attribute) and printing how managers are saved in the database and the output looked like this
Blockquote
<User: jenn>
However, even when I try filtering with that output, my query sets come up empty
Blockquote
test = Profile.objects.filter(manager='<User: jenn>')
I think I need to adjust my filter parameter to something the database can match against but I'm not sure what that format is. I've tried looking through the documentation but haven't found exactly what I'm looking for.
But that's just a string representation of a model instance. You need the actual instance.
jenn = User.objects.get(username="Jenn")
test = Profile.objects.filter(manager=jenn)
Of course, once you already have jenn as an instance, to can use the reverse accessor of the fk instead:
test = jenn.profile_set.all()
And if you don't have jenn, and you don't need it, you can do the whole thing in one query:
test = Profile.objects.filter(manager__username="Jenn")
I've also tried filtering a single profile entry (with a non-foreign key attribute) and printing how managers are saved in the database and the output looked like this
That's not how managers are saved in databases, thats just a "readable" representation of an instance user.
If you want to filter on a manager you can do something like that :
test = Profile.objects.filter(manager__pk= primary_key_of_manager)
or
temp_manager = User.objects.get(...)
test = Profile.objects.filter(manager=temp_manager)