Django reading model data in a template - database

I'm new to django and am having trouble reading data from a model in a template.
Here's the model.
class Team(models.Model):
team_name = models.CharField(max_length=30, default="Team")
created = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.team_name
The view.
def create_team(request):
team = Team.objects.all()
return render_to_response("ideas/profile.html", {'team':team})
And the template.
<h2>Available groups: {{team.team_name}} </h2>
Likelihood this is an obvious fix: 99%. Thanks anyway guys!

team is not an object, it's a queryset -- simplistically, a list of objects. Even if there's only one object in the table, it's simply a list of one. As a result, you can't just reference model attributes on it as if it were an instance of the model -- you have to pull the instance out first:
{% for t in team %}
{{ t.team_name }}
{% endfor %}
A couple of notes. As the loop shows, naming it team doesn't make sense. That implies one thing, and now we're going to loop through a single entity? Best practice here is to name single items singular and querysets, lists, etc. plural. Then we would do for team in teams which makes a lot more sense.
Secondly, don't use the model name in the attribute name. team.team_name is redundant, when team.name would work just as well. If there's another "name" attribute, then prefix that one, but the model itself should have priority on top-level attribute names.

Related

GAE: Efficiently querying entities and their referenced entities

I am trying to efficiently return a list of entities, and their respective referenced entities in a template view. For example
We are working with two Kinds:
class Event(ndb.Model):
"""An fun activity or event"""
name = ndb.StringProperty()
venue = ndb.KeyProperty()
class Venue(ndb.Model):
"""The venue of an event"""
name = ndb.StringProperty()
address = StringProperty()
The Event kind references Venue via an ndb.KeyProperty(). To display a list of events and their respective venues into a template, I can first do this query:
# we can fetch this from memcache
events = Event.query().fetch()
Then, in my view:
{% for event in events %}
Event Name: {{event.name}}
Event Venue: {{event.venue.get().name}} # is this line costly?
{% endfor %}
With this method, I think that for each event, there will be get() call for its respective venue. If this is true, it sounds expensive. Assuming that there are 100 events. Each page load would incur 100 event.venue.get().name requests. This means that a modest 10000 page views per day would incur 10000 * 100 .get() requests. Does that sound correct?
Is this the best approach to this problem? If not, what options can I consider?
First, depending on the total number of venues in your dataset, they may all easily fit into Memcache. So unless a venue is modified, you can go for days without touching the datastore - regardless of the number of page views. Make sure you use Memcache for your venues too.
Second, a more efficient way to retrieve entities is in a batch request. Loop through your events, create a list of all venues that you need (by the way, which may be smaller than the number of events if several events happen in the same venue - I don't see that check in your code), then issue a batch request for all venues.
Here is the Python code to fetch all of the venue names:
venue_keys = set(event.venue for event in events)
venues = ndb.get_multi(venue_keys)
venue_name = {venue.key, venue.name for venue in venues}
Then, in your template, you can use:
Event Venue: {{ venue_name.get(event.venue, 'No venue') }}

How do I hard-code a type into my models in Django

Here's the logic for the models:
Category. There are several categories; each category can contain several products.
Product. There are several products; each product can only have one category.
Is it possible to specify what kind of category each product is within the model file itself? For example: can I set the model so that a shirt can only be clothing and nothing else?
Here's what I have so far (it doesn't validate):
class Category(models.Model):
CATEGORY_CHOICES = (
('CLOTHING', 'Clothing'),
('FURNITURE', 'Furniture'),
)
category = models.CharField(choices=CATEGORY_CHOICES)
class Shirt(Product):
category = models.ForeignKey(Category, default=CATEGORY_CHOICES.CLOTHING)
class Table(Product):
category = models.ForeignKey(Category, default=CATEGORY_CHOICES.FURNITURE)
I'm new at this. Thanks for the help!
You can validate your model on save with any arbitrary rules. So, write a validation rule that checks that all shirts are saved in the category clothing.
For user input, create a form that only provides choices corresponding to the product.
Good luck!
I suggest that you invest some time into adopting recently added model validation, while it's not automatic as forms validation (you'll have to call clean* methods yourself, probably inside save), you gonna get DRY validation that could be used on Model and Form level.
You can use callables to give instance as a default value
Something like this (untested code):
class Shirt(Product):
def getClothingInstance():
return Category.objects.get(category=Category.CATEGORY_CHOISES['CLOTHING'])
category = models.ForeignKey(Category, default=getClothingInstance)

Get a list of all unique tags used within a set of entities

I've got two models: Group and Item. An Item has a list of tags and belongs to a Group.
class Group(db.Model):
name = db.StringProperty()
class Item(db.Model):
title = db.StringProperty()
tags = db.StringListProperty()
group = db.ReferenceProperty(Group)
So far, typical actions are adding a tag to an Item, removing a tag from an Item, and showing all Items matching a given Group and tag.
What's a good way for getting a list of all unique tags used within a Group?
Ideally I'd like to have a property in Group that reflects the tags used:
class Group(db.Model):
name = db.StringProperty()
aggregated_tags = db.StringListProperty()
It would be even better if this included the number of Items that have this tag.
Permanent consistency is not a requirement, i.e. it is fine if the aggregated list of tags does not match the actual list of tags in use, as long as they become consistent eventually.
Item and Group are not in the same entity group, so I can't have a transaction that updates the Item and the Group at the same time.
The best way to do this is to maintain it yourself. Update the list of tags whenever you add or remove one from an item. You can do this in a task queue task if you want to do it asynchronously.
Alternatively, you could write a mapreduce that you run periodically that recalculates the tag set for every group - in fact, this is pretty much a classic use-case for mapreduce.

String conversion within GAE webapp template

I am trying to create a string of the key of a ReferenceProperty within a webapp template:
Assume the following simple datastore model:
def User(db.Model):
first_name = StringProperty()
last_name = StringProperty()
def Email(db.Model):
user = ReferenceProperty(User)
email = EmailProperty()
I then pass a list of Email entities to a webapp template in list named member_list. Within the template, I want to create a string of the key of each Email entity's 'user' property for use in a URL, such as:
{% for member in member_list %}
<a href="/member_handler/{{INSERT_STRING_OF_MEMBER.USER_KEY_HERE"}}>blah</a>
I realize I could pass a string of the key to the template, but I would prefer to do the string conversion in template if possible; I have tried various permutations of str() and _ str_ to no avail.
Since you know the entity in question will be a Member instance, and presumably won't have a parent entity, it's much simpler (and produces nicer URLs) to use the key name or ID of the member, rather than the full string key. You can get this with user.key().name() (user.key.name in a Django template) or user.key().id(). Which one you need depends on whether you created the entity with a key name or not.
If you have an email object, there's no need to fetch the User from it just to get its key. Instead, call Email.user.get_value_for_datastore(member), which will return the Key object of the User it references. You can then extract the relevant field as you wish. There's no way to do this in Django, though, so you'll either need to do it outside Django and pass it in, or add a method to the Email class that returns the key.
Once you have an ID or Name and want to fetch the User object it references, call User.get_by_id(id) or User.get_by_key_name(name) as appropriate.
member.user.key should do the trick:
{% for member in member_list %}
<a href="/member_handler/{{member.user.key"}}>blah</a>
blah

DRY unique objects in Django

I want to ensure an object is unique, and to throw an error when a user tries to save it (e.g. via the admin) if not? By unique, I mean that some of the object's attributes might hold the same values as those of other objects, but they can't ALL be identical to another object's values.
If I'm not mistaken, I can do this like so:
class Animal(models.Model):
common_name = models.CharField(max_length=150)
latin_name = models.CharField(max_length=150)
class Meta:
unique_together = ("common_name", "latin_name")
But then each time I refactor the model (e.g. to add a new field, or to change the name of an existing field), I also have to edit the list of fields in the parenthesis assigned to unique_together. With a simple model, that's OK, but with a substantial one, it becomes a real hassle during refactoring.
How can I avoid having to repeat typing out the list of field names in the unique_together parenthesis? Is there some way to pass the list of the model's fields to a variable and to assign that variable to unique_together instead?
Refactoring models is a rather expensive thing to do:
You will need to change all code using your models since field names correspond to object properties
You will have to change your database manually since Django cannot do this for you (at least the version I used the last time when I worked with Django couldn't)
Therefore I think updating the list of unique field names in the model meta class is the least issue you should worry about.
EDIT: If you really want to do this and all of your fields must be "unique together", then the guy at freenode is right and you'll have to write a custom metaclass. This is quite complicated and errorprone, plus it might render your code incompatible to future releases of Django.
Django's ORM "magic" is controlled by the metaclass ModelBase (django.db.models.base.ModelBase) of the generic base class Model. This class is responsible to take your class definition with all fields and Meta information and construct the class you will be using in your code later.
Here is a recipe on how you could achieve your goal:
Subclass ModelBase to use your own metaclass.
Override the method __new__(cls, name, bases, dict)
Inspect dict to gather the Meta member (dict["Meta"]) as well as all field members
Set meta.unique_together based on the names of the fields you gathered.
Call the super implementation (ModelBase.__new__)
Use the custom metaclass for all your unique models using the magic member __metaclass__ = MyMetaclass (or derive an abstract base class extending Model and overriding the metaclass)

Resources