Ok, I'm making progress on this module, but I've run into a new
problem.
In the admin, when I try to add a Zone, I get the following error.
<class 'satchmo.l10n.models.Country'> has no ForeignKey to <class
'shipping.zone_shipping.models.Zone'>
My zone_shipping_zone_country table clearly shows the constraint
below:
CONSTRAINT zone_shipping_zone_country_country_id_fkey FOREIGN KEY
(country_id)
REFERENCES l10n_country (id) MATCH SIMPLE
I've I said before, I'm trying to link to the existing Country table
in order to not repeat myself by recreating a new one. I have
initially wanted to inherit/extend this Country model, but couldn't.
Anyway, I guess I'll include my code so far so as any one interested
could look it over and possibly help me out.
models.py
"""
zone shipper shipping models
"""
from django.db import models
from django.utils.translation import get_language, ugettext_lazy as _
from satchmo.l10n.models import Country
from satchmo.shipping.modules.base import BaseShipper
class Zone(models.Model):
name = models.CharField(max_length=25)
country = models.ManyToManyField(Country)
def __str__(self):
return self.name
class Meta:
ordering = ["name"]
class Admin:
pass
class Carrier(models.Model):
name = models.CharField(max_length=100)
active = models.BooleanField()
ordering = models.IntegerField()
delivery = models.CharField(max_length=200)
def __str__(self):
return self.name
class Meta:
ordering = ["name"]
class Admin:
pass
class Tarif(models.Model):
min_weight = models.IntegerField()
max_weight = models.IntegerField()
tarif = models.DecimalField(max_digits=6, decimal_places=2)
zone = models.ForeignKey(Zone)
carrier = models.ForeignKey(Carrier)
def __str__(self):
return "Tarif %d - %d for zone %s and carrier %s" %
(self.min_weight, self.max_weight, self.zone.name, self.carrier.name)
class Meta:
ordering = ["min_weight", "max_weight", "zone", "carrier"]
class Admin:
pass
class Shipper(BaseShipper):
def __init__(self, carrier):
self.carrier = carrier
super(BaseShipper, self).__init__(self)
def __str__(self):
"""
This is mainly helpful for debugging purposes
"""
return "Tiered_Shipper: %s" % self.id
def description(self):
"""
A basic description that will be displayed to the user when
selecting their shipping options
"""
return self.carrier.name
def cost(self):
"""
Complex calculations can be done here as long as the return
value is a dollar figure
"""
country = self.contact.shipping_address.country
weight = 0
for cartitem in self.cart.cartitem_set.all():
weight += cartitem.quantity * cartitem.product.weight
return
Tarif.objects.filter(min_weight__lte=weight).filter(max_weight__gte=weight).filter(zone__country__abbreviation=country).filter(carrier__name
= self.carrier.name)
def method(self):
"""
Describes the actual delivery service (Mail, FedEx, DHL, UPS,
etc)
"""
return self.carrier.name
def expectedDelivery(self):
"""
Can be a plain string or complex calcuation returning an
actual date
"""
return self.carrier.delivery
def valid(self, order=None):
"""
Can do complex validation about whether or not this option is
valid.
For example, may check to see if the recipient is in an
allowed country
or location.
"""
return True
__init__.py
from models import Carrier, Shipper
from satchmo.utils import load_once
load_once('tiered', 'satchmo.shipping.modules.tiered')
def get_methods():
return [Shipper(carrier) for carrier in
Carrier.objects.filter(active=True).order_by('ordering')]
admin.py
from shipping.zone_shipping.models import Carrier, Zone, Tarif
from satchmo.l10n.models import Country
from django.contrib import admin
from django.utils.translation import get_language, ugettext_lazy as _
class Carrier_Inline(admin.TabularInline):
model = Carrier
extra = 6
ordering = ('name', 'active')
class Country_Inline(admin.TabularInline):
model = Country
extra = 1
class ZoneOptions(admin.ModelAdmin):
ordering = ('name',)
inlines = [Country_Inline,]
class Zone_Inline(admin.TabularInline):
model = Zone
extra = 1
admin.site.register(Zone, ZoneOptions)
class TarifOptions(admin.ModelAdmin):
ordering = ('min_weight', 'max_weight', 'tarif')
inlines = [Carrier_Inline, Zone_Inline, ]
admin.site.register(Tarif, TarifOptions)
Also, I must admin that I don't know much about this admin.py file,
and I roughly hacked it together from the tiered admin.py - I imagine
it could be the root of my problem, as my current issue is in the
admin site.
Thanks again for all of your help,
Sam
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Satchmo users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/satchmo-users?hl=en
-~----------~----~----~----~------~----~------~--~---