I am trying to populate my form with a list of plans.
Here is my unicode for the Plans model
def __unicode__(self):
label = "ID: %s, Member(s): %s, Plan Type: %s" % (self.id, self.get_owners(), self.plan_type)
return unicode(label)
Now I call get_owners which is shown below:
def get_owners(self):
owners = self.planmember_set.filter(ownership_type__code__in=["primary","joint"])
return owners
But my output shows:
[<PlanMember: Name, [membership_type]><PlanMember: Name, etc etc>]
How do I go about displaying the output without the brackets, and more along the lines of:
Name [membership_type], Name [membership_type], etc
You're just returning the raw queryset from get_owners, and Python is calling repr() on that to insert it into the string.
The best bet is to do the formatting within get_owners:
def get_owners(self):
owners = ...
return u", ".join(unicode(o) for o in owners)
Your get_owners method is doing exactly what it should do: return a set of owners. In your template you can then loop over these owners and display them however you like:
{% for owner in plan.get_owners %}
{{ owner }}
{% endfor %}
Or, inside other python code, you can compose it into a string as you like:
def __unicode__(self):
owners = u', '.join(self.get_owners())
label = "ID: %s, Member(s): %s, Plan Type: %s" % (self.id, owners, self.plan_type)
return unicode(label)
Model methods shouldn't enforce display; they should only return data. (Except for obvious exceptions like __unicode__ which is specifically about how to display the model as unicode text.)
It looks like you need to add a __unicode__ method to PlanMember as you did for Plan.
def __unicode__(self):
label = "Name: %s, [%s]" % (self.name, self.membership_type)
return unicode(label)
Related
So i have a composite field that i want to use in my django models using postgres as the DB.
CREATE TYPE address_verbose AS (
contact_person_name string_not_null,
mobile string_no_spaces,
mobile_cc string_no_spaces,
email email_with_check,
address text,
city text,
state text,
country text,
pincode string_no_spaces,
location geography
);
The string_not_null, string_no_spaces and email_with_check are just domains i have created for validation.
Everything works fantastically if i use SQL, but the thing is want to use this field in a django models and i want to use Django ORM for at least creating, updating and deleting objects containing this field.
So after some google search i was able to come up with a custom model field.
from typing import Optional
from django.contrib.gis.geos import Point
from django.db import connection
from django.db import models
from psycopg2.extensions import register_adapter, adapt, AsIs
from psycopg2.extras import register_composite
address_verbose = register_composite(
'address_verbose',
connection.cursor().cursor,
globally=True
).type
def address_verbose_adapter(value):
return AsIs("(%s, %s, %s, %s, %s, %s, %s, %s, %s, ST_GeomFromText('POINT(%s %s)', 4326))::address_verbose" % (
adapt(value.contact_person_name).getquoted().decode('utf-8'),
adapt(value.mobile).getquoted().decode('utf-8'),
adapt(value.mobile_cc).getquoted().decode('utf-8'),
adapt(value.email).getquoted().decode('utf-8'),
adapt(value.address).getquoted().decode('utf-8'),
adapt(value.city).getquoted().decode('utf-8'),
adapt(value.state).getquoted().decode('utf-8'),
adapt(value.country).getquoted().decode('utf-8'),
adapt(value.pincode).getquoted().decode('utf-8'),
adapt(value.location).getquoted().decode('utf-8'),
))
register_adapter(address_verbose, address_verbose_adapter)
class AddressVerbose:
def __init__(self, contact_person_name: str, mobile: str, mobile_cc: str, email: str, address: str, city: str,
state: str, country: str, pincode: str, location: Optional[Point] = None):
self.contact_person_name = contact_person_name
self.mobile = mobile
self.mobile_cc = mobile_cc
self.email = email
self.address = address
self.city = city
self.state = state
self.country = country
self.pincode = pincode
self.location = location
class AddressVerboseField(models.Field):
def from_db_value(self, value, expression, connection):
if value is None:
return value
return AddressVerbose(
value.contact_person_name,
value.mobile,
value.mobile_cc,
value.email,
value.address,
value.city,
value.state,
value.country,
value.pincode,
value.location,
)
def to_python(self, value):
if isinstance(value, AddressVerbose):
return value
if value is None:
return value
return AddressVerbose(
value.contact_person_name,
value.mobile,
value.mobile_cc,
value.email,
value.address,
value.city,
value.state,
value.country,
value.pincode,
value.location,
)
def get_prep_value(self, value):
if not isinstance(value, AddressVerbose):
return None
return (
value.contact_person_name,
value.mobile,
value.mobile_cc,
value.email,
value.address,
value.city,
value.state,
value.country,
value.pincode,
# Attempt 1
value.location,
# Attempt 2
# "ST_GeomFromText('POINT(%s %s)', 4326)" % (value.location[0], value.location[1])
)
def db_type(self, connection):
return 'address_verbose'
And a minimal model that uses above field is
class Shipment(models.Model):
pickup_detail = AddressVerboseField()
drop_detail = AddressVerboseField()
Everything works completely fine in creating, updating or fetching a shipment object
if i was to remove the location field. But things start breaking when you include the location field in AddressVerboseField.
From django doc, i got the idea to fiddle with get_prep_value, and i tried the first approach by directly sending the location as is in the tuple. But it raised the
following error.
Seems the adapt function from psycopg2 was unable to parse the Point field, so thought of
directly sending the SQL text for a point field in attempt 2. This got me close but the presence of a additional quote in the sql generated broke.
I have searched quite a lot in google about adapting the composite field of postgres to django, but all results dont really include the fields present in postgis part of the postgres.
This is my first time writing a django custom field, so i might be missing or incorrect on some logic there.
It would be really hepfull if someone could help me out on this or maybe point me out in the right direction.
Thanks
I want to display answer subject and question in my template. How would I call these variables from my Answer class in my template?
Here is how my class looks
Model.py:
class Answer(models.Model):
subject = models.ForeignKey(Subject, help_text = u'The user who supplied this answer')
question = models.ForeignKey(Question, help_text = u"The question that this is an answer to")
runid = models.CharField(u'RunID', help_text = u"The RunID (ie. year)", max_length=32)
answer = models.TextField()
def __unicode__(self):
return "Answer(%s: %s, %s)" % (self.question.number, self.subject.surname, self.subject.givenname)
def choice_str(self, secondary = False):
choice_string = ""
choices = self.question.get_choices()
for choice in choices:
for split_answer in self.split_answer():
if str(split_answer) == choice.value:
choice_string += str(choice.text) + " "
Template:
{{ subject }}
{{ question }}
{{ answer }}?????
I am fairly new to Django and I am only a few weeks in to learning.
Values to the template are passed onto by views (through something known as a context) when they render some html, and not by the model classes as you seem to be indicating.
This also makes sense because model classes are just a schema or a representation of your database, whereas views are functions that retrieve values from the database (or not) and create dynamic content to be rendered.
Here's the link to the official tutorial on how to do it properly.
Pass the values in your views.py something like this:
from django.shortcuts import render_to_response
def display_variables(request):
subject = # get your subject and assign it a variable
question = # get your question and assign it a variable
answer = # get your answerand assign it a variable
return render_to_response('your_web_page.html',{'subject':subject,'question ':question ,'answer ':answer },context_instance=RequestContext(request))
I'm messing around with my first Django site and so far it's going good. I am now facing the challenge of getting some information from the DB. My model looks like this:
class Countries(models.Model):
country = models.CharField(max_length=100)
def __unicode__(self):
return self.country
class OrganisationTypes(models.Model):
organisation_type = models.CharField(max_length=100)
def __unicode__(self):
return self.organisation_type
class Organisations(models.Model):
organisation_name = models.CharField(max_length=200)
organisation_type = models.ForeignKey(OrganisationTypes)
country_of_origin = models.ForeignKey(Countries)
def __unicode__(self):
return self.organisation_name
class Locations(models.Model):
organisation = models.ForeignKey(Organisations)
country_of_location = models.ForeignKey(Countries)
tel_nr = models.CharField(max_length=15)
address = models.CharField(max_length=100)
def __unicode__(self):
return '%s - %s - %s - %s' % (self.organisation, self.country_of_location, self.tel_nr, self.address)
I now want to display a list of locations of which I want to display the organisation_name and the country_of_origin. To achieve this I wrote the following function:
def organisation_locations(requests, organisation_id):
org = Organisations.objects.get(id=organisation_id)
location_list = Locations.objects.filter(organisation=organisation_id).order_by('country_of_location')
output = '<br />'.join([str(loc.organisation)+' from '+str(org.country_of_origin) for loc in location_list])
return HttpResponse(output)
This works correctly, but it doesn't seem like the correct way of doing this. Since the Location table has a foreign key in the Organisations table which in turn has a foreign key in the Countries table I have this vague feeling that Django can do this in one "query" or lookup.
Am I correct in this feeling, or is my way indeed the correct way of doing this? All tips are welcome!
Can't you do:
location_list = Locations.objects\
.filter(organisation=organisation_id)\
.order_by('country_of_location')
output = '<br />'.join([str(loc.organisation)+' from '+str(loc.organisation.country_of_origin) for loc in location_list])
The organisation query isn't necessary. You can access organisation like this: localization.organisation.
What is not Djangonic in your code is the response. You should have a template and do return render_to_response :)
When I use the code {{ item.description }} to print the item name on cart.html page in django cartridge, It prints the item name also size..For example aviesta is the item name and size is 2.then it prints (aviesta Size: 3) ...How can i break this name and size in 2 different parts..
1. Item name
2. Item Size
I think it requires changes of the models, because when product is added to cart the name and options are saved to description:
class Cart(models.Model):
...
item.description = unicode(variation)
and
class ProductVariation(Priced):
...
def __unicode__(self):
"""
Display the option names and values for the variation.
"""
options = []
for field in self.option_fields():
if getattr(self, field.name) is not None:
options.append("%s: %s" % (unicode(field.verbose_name),
getattr(self, field.name)))
return ("%s %s" % (unicode(self.product), ", ".join(options))).strip()
UPD:
You can add field to SelectedProduct class:
options = CharField(_("Options"), max_length=200)
Add method to ProductVariation class:
def options_text(self):
options = []
for field in self.option_fields():
if getattr(self, field.name) is not None:
options.append("%s: %s" % (unicode(field.verbose_name),
getattr(self, field.name)))
return ", ".join(options).strip()
def __unicode__(self):
"""
Display the option names and values for the variation.
"""
return ("%s %s" % (unicode(self.product), self.options_text())).strip()
Change add_item method in Cart class:
item.description = unicode(variation.product)
item.options = variation.options_text()
I am trying to dynamically build a list of admin actions using the get_actions() method on a ModelAdmin. Each action relates to a particular instance of another model, and as new instances may be added or removed, I want to make sure the list of actions reflects that.
Here's the ModelAdmin:
class PackageAdmin(admin.ModelAdmin):
list_display = ('name', 'quality')
def _actions(self, request):
for q in models.Quality.objects.all():
action = lambda modeladmin, req, qset: qset.update(quality=q)
name = "mark_%s" % (q,)
yield (name, (action, name, "Mark selected as %s quality" % (q,)))
def get_actions(self, request):
return dict(action for action in self._actions(request))
(The weird repetitive dict of tuples return value is explained by the Django docs for get_actions().)
As expected, this results in a list of appropriately named admin actions for bulk assignment of Quality foreign keys to Package objects.
The problem is that whichever action I choose, the same Quality object gets assigned to the selected Packages.
I assume that the closures I am creating with the lambda keyword all contain a reference to the same q object, so every iteration changes the value of q for every function.
Can I break this reference, allowing me to still use a list of closures containing different values of q?
Edit: I realise that lambda is not necessary in this example. Instead of:
action = lambda modeladmin, req, qset: qset.update(quality=q)
I could simply use def:
def action(modeladmin, req, qset):
return qset.update(quality=q)
try
def make_action(quality):
return lambda modeladmin, req, qset: qset.update(quality=quality)
for q in models.Quality.objects.all():
action = make_action(q)
name = "mark_%s" % (q,)
yield (name, (action, name, "Mark selected as %s quality" % (q,)))
if that doesn't work, i suspect the bug has something to do with your use of yield. maybe try:
def make_action(quality):
name = 'mark_%s' % quality
action = lambda modeladmin, req, qset: qset.update(quality=quality)
return (name, (action, name, "Mark selected as %s quality" % quality))
def get_actions(self, request):
return dict([make_action for q in models.Quality.objects.all()])
As I mentioned in my comment to andylei's answer, I just found a solution; using another function to create the closure seems to break the reference, meaning that now every action refers to the correct instance of Quality.
def create_action(quality):
fun = lambda modeladmin, request, queryset: queryset.update(quality=quality)
name = "mark_%s" % (quality,)
return (name, (fun, name, "Mark selected as %s quality" % (quality,)))
class PackageAdmin(admin.ModelAdmin):
list_display = ('name', 'quality')
def get_actions(self, request):
return dict(create_action(q) for q in models.Quality.objects.all())
I am surprised that q stays the same object within the loop.
Does it work with quality=q.id in your lambda?