This is a very simple question that got me stuck. I have 2 tables that are connected: Dealershiplistings, Dealerships. On my website I need to display the Model, Make and year of the car (Which are all stored in the DealershipListing, so i have no problem wiht this part) but I also need to print the address that is stored in the Dealerships table. Can anyone help me with this?
this is what i have for my views.py
def store(request):
dealer_list = Dealer.objects.all()
car_list = DealershipListing.objects.all()
context = {'dealer_list': dealer_list, 'car_list': car_list}
return render(request, 'store/store.html', context)
i tried doing
{{%for car in car_list%}}
<h6>{{car.year}} {{car.make}} {{car.model}}</h6>
{% endfor %}
which works perfectly displaying those. But now how do i display the address of the dealership that is selling the car?
models.py
class Dealer(models.Model):
dealersName = models.TextField(('DealersName'))
zipcode = models.CharField(("zipcodex"), max_length = 15)
zipcode_2 = models.CharField(("zipCode"), max_length = 15)
state = models.CharField(("state"), max_length=5)
address = models.TextField(("Address"))
dealerId = models.IntegerField(("ids"), primary_key=True)
def __str__(self):
return self.dealersName
class DealershipListing(models.Model):
carID = models.IntegerField(("CarID"), primary_key=True)
price = models.IntegerField(('price'))
msrp = models.IntegerField(('msrp'))
mileage = models.IntegerField(('mileage'))
is_new = models.BooleanField(('isNew'))
model = models.CharField(("Models"), max_length= 255)
make = models.CharField(("Make"), max_length=255)
year = models.CharField(("Year"),max_length=4)
dealerID = models.ForeignKey(Dealer, models.CASCADE)
def __str__(self):
return self.year + " " + self.make + " " + self.model
So then it looks like your question is really How do I access data from a foreign key in a template?
The answer is refreshingly simple!
{{car.dealerID.address}}
On a side note, you might want to rename dealerID to dealer, django will handle the db column names how it sees fit, so while you might access the data with .dealer the db column would be named dealer_id by django automatically. Renaming the field also makes it more obvious that accessing it will give you a dealer and not its id.
calling with the model name is what I prefer to use
{{obj.related_table.field_name}}
I think this pattern may help you solve problem related to getting related field value
Related
I am new to django. I am working on a test project. Where I have a Model CollectFee with structure given below:
class CollectFee(models.Model):
boarder = models.ForeignKey(Boarder, on_delete=models.CASCADE)
feetype = models.ForeignKey(FeeType, on_delete=models.CASCADE)
amountdue = models.PositiveIntegerField("amount Due")
amountpaid = models.PositiveIntegerField("amount Paid")
balance = models.PositiveIntegerField("Balance")
class Meta:
verbose_name_plural = "Collect Fee"
def __str__(self):
return self.boarder.name
I want to apply a query set in the views which will display all the records where feetype_id is not 2, when this record is excluded then also exclude those records which have the same boarder_id as of already excluded record.
For example, Exclude the second row as it has feetype_id = 2 then also exclude the third row because it has the same boarder_id as of second row.
As I am new, I was able to just implement the filter below:
def feedue(request):
last_fee_type = FeeType.objects.last()
boarders = CollectFee.objects.exclude(feetype_id=last_fee_type)
context = {'boarders':boarders}
return render(request, 'fee-due.html', context)
You can exclude the many-to-one relationship with the through accessor like this:
CollectFee.objects.exclude(boarder__collectfee__feetype=last_fee_type)
This expression essentially gets the Boarder from a particular CollectFee object, then all CollectFees associated with that Boarder and their FeeTypes, allowing you to exclude accordingly.
May be this helps
def feedue(request):
excluded_objs = CollectFee.objects.filter(feetype_id=2)
border_ids = [id.border_id for obj in excluded_objs]
duefee = CollectFee.objects.exclude(boarder_id__in=border_ids)
context = {'duefee': duefee}
return render(request, 'fee-due.html', context)
I'm working on my first django project which is a sport betting game.
My models are:
class Game(models.Model):
home_team = models.CharField(max_length=200)
away_team = models.CharField(max_length=200)
home_goals = models.IntegerField(default=None)
away_goals = models.IntegerField(default=None)
class Bet(models.Model):
gameid = models.ForeignKey(Game, on_delete=models.CASCADE)
userid = models.ForeignKey(User, on_delete=models.CASCADE)
home_goals = models.IntegerField()
away_goals = models.IntegerField()
score = models.IntegerField(default=None, null=True)
logic for calculating scores is:
WHEN polls_bet.home_goals = polls_game.home_goals AND polls_bet.away_goals = polls_game.away_goals THEN 2
WHEN polls_game.home_goals > polls_game.away_goals AND polls_game.home_goals > polls_game.away_goals THEN 1
WHEN polls_bet.home_goals < polls_bet.away_goals AND polls_game.home_goals < polls_game.away_goals THEN 1
ELSE 0
I was able to solve it easily using database view that combines all data in one table but it seems that it does not play well with django migrations..
So I was thinking about sql trigger after update of game goals, but then I don't know how to pass conditions like user's id.
2nd idea is to create additional 'scores' table with:
gameid,
userid,
betid,
score
But then again I don't know how to calculate scores.
Please advice how this should be done properly, without using sql view. I appreciate all answers!
You can define a 'score' property on the Bet model to solve this easily. Please refer to the documentation here.
Your property implementation will be something like:
#property
def score(self):
if (self.home_goals == self.game__home_goals and
self.away_goals == self.game__away_goals):
return 2
if (self.game__home_goals > self.game__away_goals):
return 1
if (self.home_goals < self.away_goals and
self.game__home_goals < self.home_goals):
return 1
return 0
On a side note, the normal naming convension for a foreignkey relation is the model name in lowercase. So it becomes 'game' and 'user' instead of 'gameid' and 'userid'. Also I believe you have some typos on the second condition.
I have two models that I'm relating using Django's OneToOneField, following this documentation: https://docs.djangoproject.com/en/2.0/topics/db/examples/one_to_one/
class Seats(models.Model):
north = models.OneToOneField('User',on_delete=models.CASCADE,related_name='north', default=None, null=True)
bridgetable = models.OneToOneField('BridgeTable',on_delete=models.CASCADE, default=None, null=True)
class BridgeTableManager(models.Manager):
def create_deal(self):
deal = construct_deal()
table = self.create(deal=deal)
s = Seats(bridgetable=table)
s.save()
return table
class BridgeTable(models.Model):
deal = DealField(default=None,null=True)
When I run this code I can successfully get the relationship working
table = BridgeTable.objects.get(pk='1')
user = User.objects.get(username=username)
table.seats.north = user
table.seats.north.save()
print(table.seats.north)
The print statement prints out the name of the player sitting north. But if I try to access the table again like this:
table = BridgeTable.objects.get(pk='1')
print(table.seats.north)
I get "None" instead of the user's name. Is there something I'm missing, like a save that I missed or some concept I'm not understanding? Thanks.
You should save Seats model object that is table.seats.save()
Try print table.seats.north
While table.seats.north.save() runs save on User object
Here are correct steps:
table = BridgeTable.objects.get(pk='1')
user = User.objects.get(username=username)
table.seats.north = user
table.seats.save()
print(table.seats.north)
I have the following models
class Employee(Person):
job = model.Charfield(max_length=200)
class Address(models.Model):
street = models.CharField(max_length=200)
city = models.CharField(max_length=200)
class EmpAddress(Address):
date_occupied = models.DateField()
date_vacated = models.DateField()
employee = models.ForeignKey()
When I build a json data structure for an EmpAddress object using the django serialzer it does not include the inherited fields only the EmpAddress fields. I know the fields are available in the object in my view as I can print them but they are not built into the json structure.
Does anyone know how to overcome this?
Thanks
Andrew
Inheritance of Django models can get a little tricky. Unless you excplicitly require EmpAddress to be subclass of Address, you might just want to duplicate the fields and let duck typing handle the fact that you aren't following traditional object oriented design. E.g:
class Address(models.Model):
street = models.CharField(max_length=200)
city = models.CharField(max_length=200)
class EmpAddress(Address):
street = models.CharField(max_length=200)
city = models.CharField(max_length=200)
date_occupied = models.DateField()
date_vacated = models.DateField()
employee = models.ForeignKey()
Another shot in the dark you might try is to use jsonpickle (I'm one of the developers), which is "smarter" than the standard json module. The latest code has some great new features, thanks to davvid.
Take a look at: http://www.partisanpost.com/2009/10/django-jquery-jqgrid-example-one/1/ as a solution to your problem. The full serializer allows you to drill down into foreignkey relationships as far as you need to go. I wrote a tutorial example of how to use it to integrate django with JqGrid, which provides an example of just what you are faced with. Hope this helps.
John,
This is view code I am using along with the models is;
def address_grid(request):
employeeId = request.GET.get('employeeId')
if request.GET.get('sidx') == '':
order = 'date_occupied'
else:
order = request.GET.get('sidx')
if request.GET.get('sord') == 'asc':
sort_order = ''
else:
sort_order = '-'
order = sort_order + order
if request.GET.get('page'):
paginated = int(request.GET.get('page'))
else:
paginated = 1
items = int(request.GET.get('rows'))
addresses = EmpAddress.objects.filter(employee__id=employeeId)
for add in addresses:
log.write(add.city+'\n') # Field from address object
total = adresses.all().count()
if total % items > 0:
items_sum = 1
else:
items_sum = 0
pages = total / items + items_sum
if paginated > pages:
paginated = 1
addresses = addresses.order_by(order)[paginated-1)*items:paginated*items]
rows = serializers.serialize("json", addresses, indent=4,)
addresses = '{total:%(pages)s, page:%(page)s, records:%(total)s, rows:%(addresses)s' \
% {'pages':pages, 'page':paginated, 'total':total, 'addresses':rows}
log.write(rows+'\n') #json object no Address fields (city is not included)
#even it is present above
return HttpResonse(addresses, mimetype="application/json")
When I print the addresses objects after the
addresses = EmpAddress.objects.filter(employee__id=employeeId)
line I have all of the objects attributes (both the Address and EmpAddress fields).
But when I print the json object I only have the EmpAddress object attributes excluding the Address attributes.
What I want is to be able to get this weeks/this months/this years etc. hotest products. So I have a model named ProductStatistics that will log each hit and each purchase on a day-to-day basis. This is the models I have got to work with:
class Product(models.Model):
name = models.CharField(_("Name"), max_length=200)
slug = models.SlugField()
description = models.TextField(_("Description"))
picture = models.ImageField(upload_to=product_upload_path, blank=True)
category = models.ForeignKey(ProductCategory)
prices = models.ManyToManyField(Store, through='Pricing')
objects = ProductManager()
class Meta:
ordering = ('name', )
def __unicode__(self):
return self.name
class ProductStatistic(models.Model):
# There is only 1 `date` each day. `date` is
# set by datetime.today().date()
date = models.DateTimeField(default=datetime.now)
hits = models.PositiveIntegerField(default=0)
purchases = models.PositiveIntegerField(default=0)
product = models.ForeignKey(Product)
class Meta:
ordering = ('product', 'date', 'purchases', 'hits', )
def __unicode__(self):
return u'%s: %s - %s hits, %s purchases' % (self.product.name, str(self.date).split(' ')[0], self.hits, self.purchases)
How would you go about sorting the Products after say (hits+(purchases*2)) the latest week?
This structure isn't set in stone either, so if you would structure the models in any other way, please tell!
first idea:
in the view you could query for today's ProductStatistic, than loop over the the queryset and add a variable ranking to every object and add that object to a list. Then just sort after ranking and pass the list to ur template.
second idea:
create a filed ranking (hidden for admin) and write the solution of ur formula each time the object is saved to the database by using a pre_save-signal. Now you can do ProductStatistic.objects.filter(date=today()).order_by('ranking')
Both ideas have pros&cons, but I like second idea more
edit as response to the comment
Use Idea 2
Write a view, where you filter like this: ProductStatistic.objects.filter(product= aProductObject, date__gte=startdate, date__lte=enddate)
loop over the queryset and do somthing like aProductObject.ranking+= qs_obj.ranking
pass a sorted list of the queryset to the template
Basically a combination of both ideas
edit to your own answer
Your solution isn't far away from what I suggested — but in sql-space.
But another solution:
Make a Hit-Model:
class Hit(models.Model):
date = models.DateTimeFiles(auto_now=True)
product = models.ForeignKey(Product)
purchased= models.BooleanField(default=False)
session = models.CharField(max_length=40)
in your view for displaying a product you check, if there is a Hit-object with the session, and object. if not, you save it
Hit(product=product,
date=datetime.datetime.now(),
session=request.session.session_key).save()
in your purchase view you get the Hit-object and set purchased=True
Now in your templates/DB-Tools you can do real statistics.
Of course it can generate a lot of DB-Objects over the time, so you should think about a good deletion-strategy (like sum the data after 3 month into another model MonthlyHitArchive)
If you think, that displaying this statistics would generate to much DB-Traffic, you should consider using some caching.
I solved this the way I didn't want to solve it. I added week_rank, month_rank and overall_rank to Product and then I just added the following to my ProductStatistic model.
def calculate_rank(self, days_ago=7, overall=False):
if overall:
return self._default_manager.all().extra(
select = {'rank': 'SUM(hits + (clicks * 2))'}
).values()[0]['rank']
else:
return self._default_manager.filter(
date__gte = datetime.today()-timedelta(days_ago),
date__lte = datetime.today()
).extra(
select = {'rank': 'SUM(hits + (clicks * 2))'}
).values()[0]['rank']
def save(self, *args, **kwargs):
super(ProductStatistic, self).save(*args, **kwargs)
t = Product.objects.get(pk=self.product.id)
t.week_rank = self.calculate_rank()
t.month_rank = self.calculate_rank(30)
t.overall_rank = self.calculate_rank(overall=True)
t.save()
I'll leave it unsolved if there is a better solution.