How to create data to the database using views - django-models

I am trying to create order amount from the catalogue which works like a shopping cart but the amount returned is 1 for all orders made:
views.py
def get_user_pending_order(request):
#get order from correct profile
user_profile = get_object_or_404(Profile,user=request.user)
order = Order.objects.filter(owner=user_profile,is_ordered=True)
if order.exists():
#to get an order in the list of filtered orders
return order[0]
return 0
def add_to_catalogue(request,employee_id):#product_id,employee_id
user_profile= get_object_or_404(Profile, user =request.user)
order_to_purchase = get_user_pending_order(request)
amount= self.order_to_purchase.get_catalogue_total(),
employee = Employee.objects.get(pk=employee_id)
if employee in request.user.profile.ebooks.all():
messages.info(request,'you already own this ebook')
return redirect(reverse('freelance:employee_list'))
order_task,status =
OrderTask.objects.get_or_create(employee=employee)
user_order,status = Order.objects.get_or_create(owner=user_profile,
is_ordered=False,order_amount=amount)####TThis IS WHWERE TO EDIT TO PREVENT
RE ORDERNG OF FREELANCING
user_order.tasks.add(order_task)
if status:
user_order.ref_code = generate_order_id()
user_order.save()
messages.info(request,"task added to catalogue")
return redirect(reverse('freelance:employee_list'))
def get_user_pending_order(request):
#get order from correct profile
user_profile = get_object_or_404(Profile,user=request.user)
order = Order.objects.filter(owner=user_profile,is_ordered=True)
if order.exists():
#to get an order in the list of filtered orders
return order[0]
return 0
models.py
class Order(models.Model):
ref_code = models.CharField(max_length=15)
owner = models.ForeignKey(Profile, on_delete=models.SET_NULL, null=
True)
is_ordered = models.BooleanField(default=False)
tasks = models.ManyToManyField(OrderTask)
date_ordered = models.DateTimeField(auto_now= True)
order_amount = models.DecimalField(default=0.01, max_digits= 10,
decimal_places=2)
def order_tasks(self):
return ','.join([str(c.employee) for c in self.tasks.all()])
def get_catalogue_tasks(self):
return self.tasks.all()
def get_catalogue_total(self):
return sum([task.employee.pricing for task in self.tasks.all()])
def __str__(self):
return '{0} - {1}'.format(self.owner, self.ref_code, self.order_amount)
def tasks_summary(request):
existing_order = get_user_pending_order(request)
my_user_profile = Profile.objects.filter(user=
request.user).first()
my_orders = Order.objects.filter(is_ordered= True, owner=
my_user_profile)
order_to_purchase = get_user_pending_order(request)
amount= order_to_purchase.get_catalogue_total(),
order = Order.objects.filter(is_ordered= True)
context = {
'my_orders':my_orders,
'order':order,
'amount':amount
# 'total':total,
}
return render(request,
'freelance/tasks_summary.html',context)###Belongs to the admin sisde
Output of the template
I am getting this error when I try to add anything to the catalogue:
AttributeError at /admin/tasks_summary/
'int' object has no attribute 'get_catalogue_total'

Related

TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use technicians.set() instead

I keep getting this very same type error even though I am using set. Can someone maybe point out what I'm doing wrong in my api endpoint?
views.py
#require_http_methods(["GET", "POST"])
def api_requerimientos(request):
if request.method == "GET":
requerimientos = FormularioCliente.objects.all()
return JsonResponse(
{"requerimientos": requerimientos},
encoder=FormularioClienteEncoder,
)
elif request.method == "POST":
print("POST REQUEST HIT")
try:
content = json.loads(request.body)
except json.JSONDecodeError:
return HttpResponseBadRequest("Invalid JSON in request body")
requerimiento = FormularioCliente(**content) # Create a new FormularioCliente object
if "technicians" in content and isinstance(content["technicians"], list):
try:
technicians_id_list = content["technicians"]
technicians = Technician.objects.filter(employee_number__in=technicians_id_list)
requerimiento.technicians.set(technicians)
requerimiento.save()
print('requerimiento:', requerimiento)
except Technician.DoesNotExist:
pass
requerimiento.save() # Save the object to the database
models.py
class FormularioCliente(models.Model):
empresa = models.CharField(max_length=21, null=True, unique=True)
titulo = models.CharField(max_length=66)
descripcion = models.CharField(max_length=66)
enlace = models.URLField(null=True)
tipo = models.CharField(max_length=17, choices=TIPO_REQUIRIMIENTO, default="tecnologia")
date = models.DateField(null=True, auto_now_add=True)
time = models.TimeField(null=True, auto_now_add=True)
entrega = models.DateField(null=True)
finished = models.CharField(max_length=19, choices=TIPO_FINALIZACION, default="Abierto")
technicians = models.ManyToManyField(Technician, blank=True)
special_hours = models.SmallIntegerField(default=0)
regular_hours = models.PositiveSmallIntegerField(default=1)
total_hours = models.SmallIntegerField(default=1)
importancia = models.PositiveSmallIntegerField(default=1)
file = models.FileField(upload_to='files', blank=True)
updated = models.DateField(auto_now=True)
def __str__(self):
return f'{self.titulo}: {self.descripcion} # {self.date}'
def technicians_as_json(self):
return list(self.technicians.all().values())
class Technician(models.Model):
name = models.TextField()
employee_number = models.SmallIntegerField(unique=True, primary_key=True)
def __str__(self):
return self.name + " - " + str(self.employee_number)
encoders.py
class TechnicianEncoder(ModelEncoder):
model = Technician
properties = ["name", "employee_number"]
class FormularioClienteEncoder(ModelEncoder):
model = FormularioCliente
properties = [
"id",
"empresa",
"titulo",
"descripcion",
# "user",
"enlace",
# "tipo",
# "File",
"tipo",
"date",
"time",
"entrega",
"finished",
"technicians", # <-- Add this
"special_hours",
"regular_hours",
"total_hours",
"importancia",
"updated",
]
encoders = {"technicians": TechnicianEncoder()}
I also tried to loop through technicians queryset and add it to the instance one by one as well via the add() method but that also didn't work.
I even tried this:
techs = list(requerimiento.technicians.filter(employee_number__in=technicians_id_list).all())
requerimiento.technicians.set(techs)
None of it worked

ValueError: variable needs to have a value for field "id" before this many to many relationship can be used - Django

I created a datamodel in Django, and now I created a script to auto populate the models using web-scraped values. However when I run the script I get the following error:
ValueError: variable needs to have a value for field "id" before this many to many relationship can be used
Models.py
class Books(models.Model):
title = models.CharField(max_length=100)
def __str__(self):
return self.title
class Meta:
ordering = ['-title']
class Author(models.Model):
book = models.ManyToManyField(Books)
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=200)
def __str__(self):
return "{} {}".format(self.first_name, self.last_name)
class Meta:
ordering = ['last_name','first_name']
class Book_details(models.Model):
book = models.ForeignKey(Books,
on_delete=models.CASCADE,
null=True) # models.SET_NULL weggehaald
pages = models.CharField(max_length=250)
publ_year = models.CharField(max_length=250)
edition = models.CharField(max_length=30) # paperback, hardcover, audiobook, etc
def __str__(self):
return "{} - pages: <{}>, edition: <{}>".format(self.book.title,
self.pages,
self.edition)#
class Cover(models.Model):
book = models.OneToOneField(Books,
on_delete=models.CASCADE)
path = models.CharField(max_length=500)
def __str__(self):
return "<Cover <path={}>".format(self.id, self.path)
populate_script
def add_book(title):
b = Books.objects.get_or_create(title = title)[0]
print(b)
b.save()
return b
def populate(scraped_tuple):
fake = Faker()
for _ in range(len(scraped_tuple)):
b_title = scraped_tuple[_][0][0]
new_book = add_book(b_title)
b_author_first = scraped_tuple[_][0][1].split(" ")[0]
b_author_last = scraped_tuple[_][0][1].split(" ")[1]
b_pages = scraped_tuple[_][0][2].split(" ")[0]
b_publ_year = fake.year()
b_edition = scraped_tuple[_][0][3].split(",")[0]
b_cover = scraped_tuple[_][0][4]
new_details = Book_details.objects.get_or_create(book = new_book, pages = b_pages, publ_year = b_publ_year, edition = b_edition)[0]
new_author = Author.objects.get_or_create(book = new_book, first_name = b_author_first, last_name = b_author_last)[0]
new_cover = Cover.objects.get_or_create(book = new_book, path = b_cover)[0]
The scraped_tuple is a return value from the webscraper containing the details.
(Part of) the Traceback:
Books.models.DoesNotExist: Author matching query does not exist.
File "C:\path\to\LibraryApp\Library_WebA
pp\Library\populate.py", line 45, in populate
new_author = Author.objects.get_or_create(book = new_book, first_name = b_author_first, last_nam
e = b_author_last)[0]
Followed by:
ValueError: "<Author: Mary McCarthy>" needs to have a value for field "id" before this many-to-many relationship can be used.
So, it seems that something goes awfully wrong when trying to execute the new_author statement, because of the many-to-many field "book" in the Author model. How can I resolve this. Do I need a similar function for an Author object like I have for the Book in add_book()?
It seems the new_details statement executes just fine (title and book_details appear correctly in the database in the admin part of Django).
As mentioned in the docs, user .add() to associate the records in many to many field.
def populate(scraped_tuple):
fake = Faker()
for _ in range(len(scraped_tuple)):
b_title = scraped_tuple[_][0][0]
new_book = add_book(b_title)
b_author_first = scraped_tuple[_][0][1].split(" ")[0]
b_author_last = scraped_tuple[_][0][1].split(" ")[1]
b_pages = scraped_tuple[_][0][2].split(" ")[0]
b_publ_year = fake.year()
b_edition = scraped_tuple[_][0][3].split(",")[0]
b_cover = scraped_tuple[_][0][4]
new_details = Book_details.objects.get_or_create(book = new_book, pages = b_pages, publ_year = b_publ_year, edition = b_edition)[0]
new_author = Author.objects.get_or_create(first_name = b_author_first, last_name = b_author_last)[0]
# add many to many fields this way:
new_author.book.add(new_book)
new_cover = Cover.objects.get_or_create(book = new_book, path = b_cover)[0]

Wagtail Snippets permissions per group

I have a Wagtail site where every group can work on a different page tree, with different images and documents permissions.
That is a multisite setup where I am trying to keep sites really separate.
Is that possible to limit the snippets permissions on a per-group basis?
I would like my groups to see just a subset of the snippets.
I was facing something similar when I wanted to use Site settings.
The only solution I found was to create a custom model and using ModelAdmin.
Some ‘snippets’ to get you on the run:
class SiteSettings(models.Model):
base_form_class = SiteSettingsForm
COMPANY_FORM_CHOICES = (
('BED', 'Bedrijf'),
('ORG', 'Organisatie'),
('STI', 'Stichting'),
('VER', 'Vereniging'),
)
site = models.OneToOneField(
Site,
unique = True,
db_index = True,
on_delete = models.CASCADE,
verbose_name = _('site'),
related_name = 'site_settings',
help_text = _('The sites these setting belong to.')
)
company_name = models.CharField(
_('company name'),
blank = True,
max_length = 50,
help_text = _('De naam van het bedrijf of de organisatie.')
)
company_form = models.CharField(
_('company form'),
max_length = 3,
blank = True,
default = 'COM',
choices = COMPANY_FORM_CHOICES
)
...
class MyPermissionHelper(PermissionHelper):
def user_can_edit_obj(self, user, obj):
result = super().user_can_edit_obj(user, obj)
if not user.is_superuser:
user_site = get_user_site(user)
result = user_site and user_site == obj.site
return result
class SiteSettingsAdmin(ThumbnailMixin, ModelAdmin):
model = SiteSettings
menu_label = _('Site settings')
menu_icon = 'folder-open-inverse'
add_to_settings_menu = True
list_display = ['admin_thumb', 'company_name', 'get_categories']
list_select_related = True
list_display_add_buttons = 'site'
thumb_image_field_name = 'logo'
thumb_col_header_text = _('logo')
permission_helper_class = MyPermissionHelper
create_view_class = CreateSiteSettingsView
...
class CreateSiteSettingsView(SiteSettingsViewMixin, CreateView):
#cached_property
def sites_without_settings(self):
sites = get_sites_without_settings()
if not sites:
messages.info(
self.request,
_('No sites without settings found.')
)
return sites
def dispatch(self, request, *args, **kwargs):
if request.user.is_superuser and not self.sites_without_settings:
return redirect(self.url_helper.get_action_url('index'))
return super().dispatch(request, *args, **kwargs)
def get_initial(self):
initial = super().get_initial().copy()
current_site = self.request.site
initial.update({
'company_name': current_site.site_name}
)
if self.request.user.is_superuser:
initial.update({
'site': current_site}
)
return initial
def get_form(self):
form = super().get_form()
flds = form.fields
if self.request.user.is_superuser:
fld = form.fields['site']
fld.queryset = self.sites_without_settings.order_by(
Lower('site_name')
)
return form
def form_valid(self, form):
instance = form.save(commit=False)
if not self.request.user.is_superuser:
instance.site = self.request.site
instance.save()
messages.success(
self.request, self.get_success_message(instance),
buttons=self.get_success_message_buttons(instance)
)
return redirect(self.get_success_url())

Django model inserting foreign key from same model

I am building a simple comment app in Django. The app allows replies to comments and uses the same model to store comments and replies. My issues is when I try to insert a new reply, the parentpost(FK to parent comment) inserts as NULL. When I use the admin interface to insert a reply, it properly stores the parentpost ID for the parentpost I choose. So I know the issue is not within my model but within my view.
/MODEL/
class UserPost(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(max_length=50, unique=True,
help_text='Unique value for product page URL, created from name.', editable = False)
post = models.TextField()
is_active = models.BooleanField(default=True)
meta_keywords = models.CharField("Meta Keywords", max_length=255, blank = True, null = True,
help_text='Content for description meta tag')
meta_description = models.CharField(max_length = 255, blank = True, null = True,
help_text = 'Content for description meta tag')
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
parentpost = models.ForeignKey('self', blank = True, null = True)
class Meta:
#app_label = ''
db_table = 'userposts'
ordering = ['created_at']
verbose_name_plural = 'UserPosts'
def __unicode__(self):
return self.name
#models.permalink
def get_absolute_url(self):
return ('lync_posts', (), {'posts_slug': self.slug})
def save(self):
if not self.id:
d = datetime.datetime.now()
s = d.strftime('%Y-%M-%d-%H-%M-%S-%f')
slugfield = str(self.name + s)
self.slug = slugfield
super(UserPost, self).save()
/VIEW/
def reply(request, slugIn):
parentpostIn = UserPost.objects.get(slug = slugIn)
pid = parentpostIn.id
template_name = 'reply.html'
if request.method == 'POST':
form = forms.ReplyPostForm(data = request.POST)
# create a new item
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
if form.is_valid():
nameIn = form.cleaned_data['name']
postIn = form.cleaned_data['post']
newPost = UserPost(name = nameIn, post = postIn, parentpost = pid)
newPost.save()
return render_to_response(template_name, locals(), context_instance = RequestContext(request))
else:
# This the the first page load, display a blank form
form = forms.NewPostForm()
return render_to_response(template_name, locals(), context_instance=RequestContext(request))
return render_to_response(template_name, locals(), context_instance=RequestContext(request))
You are trying to set the parentpost ForeignKey by id.
You should either use:
newPost = UserPost(name = nameIn, post = postIn, parentpost = parentpostIn)
or (see Django: Set foreign key using integer?):
newPost = UserPost(name = nameIn, post = postIn)
newPost.parentpost_id = pid

Can't access model attribute from other model in Django

I need to access an attribute from a related model (to use as a filter attribute in an admin), but I get this error: 'EventTimeAdmin.list_filter[1]' refers to field 'event__sites' that is missing from model 'EventTime'.
Here is are the relevant classes from models.py:
class Event(models.Model):
title = models.CharField(max_length=100)
short_description = models.CharField(max_length=250, blank=True)
long_description = models.TextField(blank=True)
place = models.ForeignKey(Place, related_name="events", default=0, blank=True, null=True)
one_off_place = models.CharField('one-off place', max_length=100, blank=True)
phone = models.CharField(max_length=40)
cost_low = models.DecimalField('cost (low)', max_digits=7, decimal_places=2, blank=True, null=True)
cost_high = models.DecimalField('cost (high)', max_digits=7, decimal_places=2, blank=True, null=True)
age_lowest = models.PositiveSmallIntegerField('lowest age', blank=True, null=True, help_text='Use 0 for "all ages". Leave blank if no age information is available') # Not every event submits info about age limits
priority = models.ForeignKey(EventPriority)
ticket_website = models.URLField('ticket Web site', blank=True)
posted_date = models.DateTimeField('date entered', default=datetime.datetime.now())
updated_date = models.DateTimeField('date updated', editable=False, blank=True)
small_photo = models.ImageField(upload_to='img/events/%Y', blank=True)
teaser_text = models.TextField(max_length=1000, blank=True)
is_ongoing_indefinitely = models.BooleanField(db_index=True, help_text="If this box is checked, the event won't be displayed by default in the event search. Users will have to manually select \"Display ongoing events\" in order to display it.")
is_national_act = models.BooleanField('is a national marquee touring act')
categories = models.ManyToManyField(EventCategory, related_name="events")
bands = models.ManyToManyField(Band, related_name="events", blank=True)
sites = models.ManyToManyField(Site)
related_links = generic.GenericRelation(RelatedLink)
generic_galleries = generic.GenericRelation(GalleryRelationsPiece)
staff_photographer_attending = models.BooleanField('Staff Photographer will be attending')
index = EventIndex()
class Meta:
ordering = ('title',)
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
self.updated_date = datetime.datetime.now()
super(Event, self).save(*args, **kwargs)
def get_absolute_url(self):
if self.is_recurring():
return reverse('ellington_events_ongoing_detail', args=[self.id])
else:
next = self.get_next_time()
if next:
return next.get_absolute_url()
else:
try:
last = self.event_times.order_by("-event_date", "start_time")[0]
return last.get_absolute_url()
except IndexError:
return reverse('ellington_events_todays_events', args=[])
#property
def _sites(self):
"""A property used for indexing which sites this object belongs to."""
return [s.id for s in self.sites.all()]
# def start_time(self):
# return self.event_times.start_time
# TODO: Redundant
def get_absolute_url_recurring(self):
"Returns the absolute URL for this event's recurring-event page"
return reverse('ellington_events_ongoing_detail', args=[self.id])
def is_recurring(self):
"""Returns ``True`` if this event is a recurring event."""
return bool(self.recurring_event_times.count())
def get_cost(self):
"Returns this event's cost as a pretty formatted string such as '$3 - $5'"
if self.cost_low == self.cost_high == 0:
return 'Free'
if self.cost_low == self.cost_high == None:
return ''
if self.cost_low == 0 and self.cost_high == None:
return 'Free'
elif self.cost_low == None and self.cost_high == 0:
return 'Free'
if self.cost_low == self.cost_high:
cost = '$%.2f' % self.cost_low
elif self.cost_low == 0 or self.cost_low == None:
cost = 'Free - $%.2f' % self.cost_high
elif self.cost_high == 0 or self.cost_high == None:
cost = '$%.2f' % self.cost_low
else:
cost = '$%.2f - $%.2f' % (self.cost_low, self.cost_high)
return cost.replace('.00', '')
def get_age_limit(self):
"Returns this event's age limit as a pretty formatted string such as '21+'"
if self.age_lowest is None:
return 'Not available'
if self.age_lowest == 0:
return 'All ages'
return '%s+' % self.age_lowest
def get_place_name(self):
"Returns this event's venue, taking into account the one_off_place field."
if self.one_off_place:
return self.one_off_place
if self.place is not None:
return self.place.name
else:
return ""
def get_next_time(self):
"""
Return the next (in the future) ``EventTime`` for this ``Event``.
Assumes the event is not recurring. Returns ``None`` if no next time is
available.
"""
now = datetime.datetime.now()
for et in self.event_times.filter(event_date__gte=now.date()).order_by("event_date", "start_time"):
try:
dt = datetime.datetime.combine(et.event_date, et.start_time)
except TypeError:
return None
if dt >= now:
return et
return None
# TODO: proprietary
def get_lj_category(self):
"""
Returns either 'Nightlife', 'Music', 'Theater', 'Misc.' or
'Museums and Galleries' for this event.
"""
MAPPING = (
('Activities', 'Misc.'),
('Art', 'Museums and Galleries'),
('Children', 'Misc.'),
('Community', 'Misc.'),
('Culinary', 'Misc.'),
('KU calendar', 'Misc.'),
('Lectures', 'Misc.'),
('Museums', 'Museums and Galleries'),
('Performance', 'Misc.'),
)
# "Performance | Theater" is a special case.
for c in self.categories.all():
if str(c) == "Performance | Theater":
return 'Theater'
parent_categories = [c.parent_category for c in self.categories.all()]
for parent_category, lj_category in MAPPING:
if parent_category in parent_categories:
return lj_category
# Failing that, we must be in either 'Music', which can be either
# 'Music' or 'Nightlife', depending on the venue.
if self.one_off_place:
return 'Music'
# If it's downtown, it's 'Nightlife'. Otherwise, it's 'Music'.
if "downtown" in self.place.neighborhood.lower():
return 'Nightlife'
return 'Music'
class EventTime(models.Model):
event = models.ForeignKey(Event, related_name="event_times")
event_date = models.DateField('date')
start_time = models.TimeField(blank=True, null=True)
finish_time = models.TimeField(blank=True, null=True)
objects = EventTimeManager()
def long_desc(self):
return self.event.long_description
# def sites(self):
# return self.event._sites
# def sites(self):
# return self.event.sites
# #property
# def _sites(self):
# """A property used for indexing which sites this object belongs to."""
# return [s.id for s in self.event.sites.all()]
# def _sites(self):
# """A property used for indexing which sites this object belongs to."""
# return [s.id for s in self.event.sites.all()]
# def _sites(self):
# """A property used for indexing which sites this object belongs to."""
# return [s.id for s in event.sites.all()]
class Meta:
ordering = ('event_date', 'start_time')
def __unicode__(self):
return u"%s -- %s at %s" % (self.event.title, self.event_date.strftime("%m/%d/%y"), self.event.get_place_name())
def get_absolute_url(self):
year = self.event_date.strftime("%Y")
month = self.event_date.strftime("%b").lower()
day = self.event_date.strftime("%d")
return reverse('ellington_events_event_detail', args=[year, month, day, self.event.id])
def get_time(self):
"Returns this event's time as a pretty formatted string such as '3 p.m. to 5 p.m.'"
if self.start_time is not None and self.finish_time is not None:
return '%s to %s' % (dateformat.time_format(self.start_time, 'P'), dateformat.time_format(self.finish_time, 'P'))
elif self.start_time is not None:
return dateformat.time_format(self.start_time, 'P')
return 'time TBA'
def get_part_of_day(self):
"""
Returns a string describing the part of day this event time is taking
place -- either 'Morning', 'Afternoon', 'Evening' or 'Night'.
"""
if self.start_time is None:
return 'Time not available'
if 3 < self.start_time.hour < 12:
return 'Morning'
elif 12 <= self.start_time.hour < 18:
return 'Afternoon'
elif 18 <= self.start_time.hour < 21:
return 'Evening'
return 'Night'
def is_in_the_past(self):
return self.event_date < datetime.date.today()
def happens_this_year(self):
return datetime.date.today().year == self.event_date.year
def get_other_event_times(self):
"Returns a list of all other EventTimes for this EventTime's Event"
if not hasattr(self, "_other_event_times_cache"):
self._other_event_times_cache = [et for et in self.event.event_times.order_by("event_date", "start_time") if et.id != self.id]
return self._other_event_times_cache
# TODO: proprietary
def get_weather_forecast(self):
"Returns a weather.forecast.DayForecast object for this EventTime"
try:
from ellington.weather.models import DayForecast
except ImportError:
return None
if not self.event.place.is_outdoors:
raise DayForecast.DoesNotExist
if not hasattr(self, "_weather_forecast_cache"):
self._weather_forecast_cache = DayForecast.objects.get(full_date=self.event_date)
return self._weather_forecast_cache
DAY_OF_WEEK_CHOICES = (
(0, 'Sunday'),
(1, 'Monday'),
(2, 'Tuesday'),
(3, 'Wednesday'),
(4, 'Thursday'),
(5, 'Friday'),
(6, 'Saturday'),
)
The thing is, event__title is accessed from EventTimeAdmin:
class EventTimeAdmin(admin.ModelAdmin):
list_display = ('event', 'event_date', 'start_time', 'finish_time','long_desc')
list_filter = ('event_date','event__sites')
search_fields = ('event__title', 'event_date')
ordering = ('-event_date',)
class Meta:
model = EventTime
But event__sites is not accessible... why is that?
Edit: You can see my attempts (commented out) to define sites in models.py above. None of these worked.
event__sites is a ManyToManyField
You could try the following:
admin.py
def get_sites(self):
sites = self.event.sites.all()
result = u''
for site in sites:
result = u'%s - %s' % (result, site.name) # unsure about NAME - is it a valid field of Sites Model?
return result
get_sites.short_description = u'Sites'
class EventTimeAdmin(admin.ModelAdmin):
list_filter = ('event_date', get_sites)
admin.site.register(EventTime, EventTimeAdmin)
Hope it helps.

Resources