I'm trying out Django/Mezzanine and if I have a custom user profile as such:
class UserProfile(models.Model):
user = models.OneToOneField("auth.User")
street_address1 = models.CharField(max_length=100)
street_address2 = models.CharField(max_length=100)
postalcode = models.CharField(max_length=10)
city = models.CharField(max_length=32)
country = models.CharField(max_length=2)
phone = models.CharField(max_length=15)
Mezzanine creates a sign up form at account/signup/ and I would like to modify the Country field to have a drop down list of countries from a table or xml file. The foreign key is a two character field.
How should go about doing this? Do I create a model form or try to extend the right template (tried looking at accounts\templates\account_form.html but don't think it is there?
I believe if you defined a "choices" arg for the field, it'll do just that:
https://docs.djangoproject.com/en/dev/ref/models/fields/#choices
A quick Google search will probably also reveal some pre-built packages for country lists.
Related
I liked the way how django-shop is creating new products attributes by developping a product model. Ex: SmartPhone... I would like to add products attributes the same way but I do not know by where to start. By experience, when I am copying code from an app, I end up deleting the app as it doesn't work correctly.
My product model is:
`class Product(models.Model):
name = models.CharField('name', max_length=32)
slug = models.SlugField('slug', max_length=32)
description = models.TextField('description')
class Meta:
ordering = ['name']`
It would be great if you could advice me on how to add similar products attributes. This way I could create attributes like this example. I don't want to copy all the app because there is a lot of things that I don't need. [Smartcard example][1]https://github.com/awesto/django-shop/tree/master/example/myshop/models
First of all, you have to decide, whether you need a polymorphic approach or not. I assume your products do not vary that much, hence you don't need polymorphism.
Therefore something such as the smartcard example should be enough:
from shop.money.fields import MoneyField
from shop.models.product import BaseProduct, BaseProductManager, CMSPageReferenceMixin
from shop.models.defaults.mapping import ProductPage, ProductImage
class Product(CMSPageReferenceMixin, BaseProduct):
# common product fields
product_name = models.CharField(max_length=32)
slug = models.SlugField()
unit_price = MoneyField(decimal_places=3)
description = models.TextField("Description")
objects = BaseProductManager()
In my module i have two models, book and room. In room i will keep room details. In book i will keep the booking details of room. I have a many2one field in book model relating to room model. When i save the record of book, near to the many2one field a redirecting button will come. On clicking that, it will open the form view of room model. I want to make the second form view to be read only. How can i do that?. I tried to keep only read access for room model, then i can't able to save book model record. So how can i complete
class room(models.Model):
_name = 'room'
name = fields.Char('room name')
class book(models.Model):
_name = 'book'
name = fields.Char('booking person')
time = fields.Datetime('time')
room_name = fields.Many2one('room','room name')
Hello First take one boolean(like temp_bool) in room object,
now in book object, when you select room then write temp_bool field as True in selected room,
now based on this boolean you can give attrs in room object like attrs="{'readonly': [('temp_bool','=','True')]}"
I have too classes and two non-TYPO3-tables. I defined a non-TYPO3-table as a table without uid, pid, etc. columns.
My two classes:
class Tx_Abc_Domain_Model_Location extends Tx_Extbase_DomainObject_AbstractEntity
class Tx_Abc_Domain_Model_Facility extends Tx_Extbase_DomainObject_AbstractEntity
My two tables (with columns):
locations
zipcode
city
facility_id
facilities
facility_id
name
I've mapped the attributes like this:
config.tx_extbase.persistence.classes {
Tx_Abc_Domain_Model_Location.mapping {
tableName = locations
columns {
zipcode.mapOnProperty = zipcode
city.mapOnProperty = city
facility_id.mapOnProperty = facility
}
}
Tx_Abc_Domain_Model_Facility.mapping {
tableName = facilities
columns {
facility_id.mapOnProperty = uid
name.mapOnProperty = name
}
}
}
My problem:
The facility attribute of my location model got the type Tx_Abc_Domain_Model_Facility and when I'm looking for a location via the LocationRepository it builds me a location model which contains a facility model.
The problem appears, when I the search I am doing returns several results. i.e. the location with the zipcode 12345 has two different facilities (and the table locations got two rows with different facility_ids), then I would expect to get two location models and each of it got the right facility model.
But instead I get the two location models, which have all same facility model inside. They've got all the facility of the first found location.
Even if I change the type of the facility attribute to integer, there are the wrong ids. But if I enable raw query result in repository I get the correct ids.
I get also the correct ids or models, when I add to both tables an uid-column.
Is there no possibility to map tables without uid column with Extbase models?
Thanks.
Okay, the answer to my last question is: Yes, there is no possibility to map tables without uid column with Extbase models.
There is an existing ticket on forge: http://forge.typo3.org/issues/25984
The reason seems to be the hardcoded $row['uid'] in mapSingleRow() method in Tx_Extbase_Persistence_Mapper_DataMapper class.
If it's not alot of tables you have to map, a work-around could be to create views for those tables to just map the uid.
I.e.:
CREATE VIEW tx_abc_domain_model_facility AS
SELECT facility_id AS uid, facilities.* FROM facilities;
Is there a way of returning items from a database in django-nonrel, using 'order_by' on a foreignkey?
Full details are as follows:
#Models.py
class Post(models.Model):
article = models.TextField(help_text='Paste or type HTML in here')
pub_date = models.DateField()
....
class TagItems(models.Model):
title = models.CharField(max_length=200)
....
class TagRel(models.Model):
the_post = models.ForeignKey('Post')
the_tag = models.ForeignKey('Tag')
TagRel defines a ManytoMany relationship between Post and TagItems classes.
I am wanting to get a list of articles for each tag.
#Desire output
My tag
-my first post
-my second post
My second tag
- my other post
- another post
All is good so far, as I use the following to filter the data:
def tagged_posts():
tag_items = TagItems.objects.all()
li =[]
for item in tag_items:
tag_rel_item = TagRel.objects.filter(the_tag__pk = item.pk)
li.append(tag_rel_item)
return {'list_of_objects': li}
I am using db-indexer to define the filter part of the query in db-indexes.py. All this works fine but I want to order my posts by publication dates.
Django docs tell me to use:
TagRel.objects.filter(the_tag__pk = item.pk).order_by('the_tag__pub_date')
But the order_by('the_tag__pub_date') part does not appear to be supported by django-nonrel.
The following also works in normal Django:
TagRel.objects.filter(the_tag__pk = item.pk).order_by('the_post')
This works because the Posts are already sorted by date in the model.
But this also does not appear to work in django-nonrel.
So my question is how do I return my posts ordered by date (latest>oldest)?
Thanks in advance
I'm taking a guess at this - you're using a ManyToManyField. I believe that's implemented using a ListProperty on App Engine's datastore.
See the section in the datastore documentation labeled "Properties With Multiple Values Can Have Surprising Behaviors":
http://code.google.com/appengine/docs/python/datastore/queries.html
That's most likely why your results appear unsorted. ManyToMany relations aren't supported natively in GAE. You'd probably have to sort them yourself after you get the results back.
I have a table with countrycodes, whats the best way to store these 2 digit codes, just in a regular property or in the key_name property?
I thought using key_name will produce faster performance?
Ton.
Whether or not you want to use key_name depends on how you want to use it. I'm not entirely certain how you want to use these country codes based on the information in your question. Let's say, however, that you want to be able to look up the full name of the country based on the two-letter code, for instance, to get "United States" from "us". In that case, you could have an entity like this:
class CountryName(db.Model):
name = db.StringProperty()
#classmethod
def lookup_by_code(cls, code):
country_name = ""
country = cls.get_by_key_name(code)
if country is not None:
country_name = country.name
return country_name
Now, this code is not complete; you'd need to populate the model with data, and this approach does not allow for reverse lookups -- getting the code when you have the country name, but this should give you an idea of how you might be able to use key_names. Doing it this way would be substantially faster then, say, having a model that contained both code and name as fields.