autocomplete-light for adding popup outside the admin - django-forms

I'm using django-crispy-forms and would like to use autocomplete-light but can't get it going. I need users to be able to create a new facility if the one they want doesn't exist.
I just have no idea how to use autocomplete-light and I've been struggling for days. Can someone please point me in the right direction??
models.py
class CollectionFacility(TimeStampedModel):
"""
Data collection facility.
"""
facility_name = models.CharField(max_length=256, blank=False)
address_line1 = models.CharField("Address line 1", max_length=45)
address_line2 = models.CharField("Address line 2", max_length=45, blank=True)
country = models.CharField(max_length=50, blank=False)
state_province = models.CharField(max_length=100, blank=True)
city = models.CharField(max_length=100, blank=False)
postal_code = models.CharField("Postal Code", max_length=20, blank=True)
facility_contact = models.ForeignKey('FacilityContact', related_name='collection_facilities', null=True, blank=True)
def __unicode__(self):
return "%s, %s" % (self.facility_name, self.country)
class Meta:
ordering = ['country', 'facility_name', 'city', 'state_province']
verbose_name = "Collection Facility"
verbose_name_plural = "Collection Facilities"
class FacilityContact(TimeStampedModel):
TITLES = (
('Mrs.', 'Mrs.'),
('Ms.', 'Ms.'),
('Mr.', 'Mr.'),
('Dr.', 'Dr.'),
)
first_name = models.CharField(max_length=256, blank=False)
middle_initial = models.CharField(max_length=4, blank=True)
last_name = models.CharField(max_length=256, blank=False)
title = models.CharField(max_length=4, choices=TITLES, blank=True)
email = models.EmailField(blank=False)
def __unicode__(self):
return "%s, %s" % (self.last_name, self.first_name)
class Meta:
ordering = ['last_name', 'first_name']
verbose_name = "Facility Contact"
verbose_name_plural = "Facility Contacts"
forms.py
class FacilityForm(autocomplete_light.ModelForm):
class Meta:
model = CollectionFacility
views.py
facility_form = FacilityForm()
# pass it in the context to template
....
template.html
{% crispy facility_form %}

Did you check the non_admin_add_another example app ?
Docs about that one have not yet been ported to v2 which mean the code in the docs might not work. However note that autocomplete_light.example_apps.non_admin_add_another should work.
I recommend you start fiddling with that example directly in autocomplete_light's test_project, see: http://django-autocomplete-light.readthedocs.org/en/stable-2.x.x/demo.html

Related

Django forms update instance while keep date/time unchanged

I've created a model for properties and connected it to a form. I started to render the form with template tags using {{ form.as_p }} and everything worked fine when I used my view for editing/updating an instance. But I wanted to customize my html so I manually rendered all form fields and now there is an issue when I try to update/edit a specific instance. The error says:
list_date
Enter a valid date/time
The thing is that I don't want the list_date to be able to be update by the user so I just have it as a hidden input (list_date should be the value as when the instance was created). Is it something in the html that is wrong? Can I solve this by handling it in my view funtion updateProperty?
models.py
from django.db import models
from django.utils import timezone
from accounts.models import User
class Property(models.Model):
realtor = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, default=User)
title = models.CharField(max_length=200)
property_type = models.CharField(max_length=200)
address = models.CharField(max_length=200)
area = models.CharField(max_length=200, blank=True)
city = models.CharField(max_length=200)
county = models.CharField(max_length=200, blank=True)
municipality = models.CharField(max_length=200, blank=True)
zipcode = models.CharField(max_length=200, blank=True)
floor = models.IntegerField(blank=True)
description = models.TextField(blank=True)
price = models.IntegerField()
rooms = models.IntegerField()
square_meter = models.IntegerField()
balcony = models.BooleanField(blank=True)
elevator = models.BooleanField(blank=True)
fee = models.IntegerField(blank=True)
photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo1 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
photo2 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
is_published = models.BooleanField(default=True)
list_date = models.DateTimeField(default=timezone.now, blank=True)
viewing_date = models.DateTimeField(timezone.now, null=True, blank=True)
viewing_date2 = models.DateTimeField(timezone.now, null=True, blank=True)
KOMMANDE = 'KO'
TILL_SALU = 'TS'
BUDGIVNING = 'BG'
SALD = 'SA'
PROPERTY_STATUS = [
(KOMMANDE, 'Kommande'),
(TILL_SALU, 'Till Salu'),
(BUDGIVNING, 'Budgivning'),
(SALD, 'Såld'),
]
property_status = models.CharField(
max_length=2,
choices=PROPERTY_STATUS,
default=KOMMANDE,
)
def __str__(self):
return self.title
forms.py
from django.forms import DateInput, ModelForm
from .models import Property
class PropertyForm(ModelForm):
class Meta:
model = Property
fields = '__all__'
widgets = {'list_date' : DateInput()}
views.py
from django.shortcuts import get_object_or_404, render, redirect
from .models import Property
from .forms import PropertyForm
from django.utils import timezone
def createProperty(request):
form = PropertyForm()
if request.method == "POST":
form = PropertyForm(request.POST)
print(request.POST)
if form.is_valid():
new_property = form.save(commit=False)
new_property.list_date = timezone.now()
new_property.realtor = request.user
new_property.save()
print(new_property.list_date)
return redirect('/properties/')
context = {'form':form}
return render(request, 'properties/property_form.html', context)
def updateProperty(request, pk):
property = Property.objects.get(id=pk)
form = PropertyForm(instance=property)
if request.method == "POST":
form = PropertyForm(request.POST, instance=property)
if form.is_valid():
updated_property = form.save(commit=False)
updated_property.save()
return redirect('/properties/')
context = {'form':form, 'property':property}
return render(request, 'properties/property_form.html', context)
property_form.html (only some parts)
<label for="{{ form.list_date.id_for_label }}" hidden></label>
<input type="hidden" name="{{ form.list_date.html_name }}" id="{{ form.list_date.id_for_label }}" {% if property %} value="{{ property.list_date }}" {% endif %}>
I want the list_date to be unchanged when editing and submitting the form.
I've tried to render the list_date in the hidden input as above. I'm very grateful for any solutions that will help me.

DRF read_only fields not being ignored when saving

I am using Django 2.2.23 and Django Rest Framework 3.7.7. I have models and serialisers as follows:
class PurchaseOrder(models.Model):
uid = models.UUIDField(primary_key=True, default=uuid4, editable=False)
order_number = models.IntegerField(null=True, blank=True)
contact = models.ForeignKey(SupplierContact, on_delete=models.SET_NULL, null=True)
class SupplierContact(models.Model):
uid = models.UUIDField(primary_key=True, default=uuid4, editable=False)
name = models.CharField(max_length=100, null=True, blank=True)
class PurchaseOrderSerializer(serializers.ModelSerializer):
contact_name = serializers.CharField(read_only=True, source='contact.name', default='')
class Meta:
model = PurchaseOrder
fields = ['uid', 'order_number', 'contact_name']
Doing
serializer = PurchaseOrderSerializer({'order_number': 1234})
serializer.save()
results in *** ValueError: Cannot assign "{'name': ''}": "PurchaseOrder.contact" must be a "SupplierContact" instance.
where I would've thought the read_only=True on the contact_name field would exclude it from saving. What am I missing?

Saving(create,update) along with foreignkey value from another model which is related user model

It may be a challenging question if you didn't get rightly. Here I have three models in which department model should be created by taking its place name from Place model which is related to the staff model. The Staff Model is in a OneToOneField relationship with User, so when a user creates a department the place name should be passed like HiddenField in HTML . This place name is related to place model with the user with GenericForeignKey. i have created a serializer which is not working as expected, it is returning the place name ,
.
In shortly I want to create a department while place should be selected from current user ID
class Staff(BaseModel):
ROLES = [
('ADMIN', 'Admin'),
('TEACHER', 'Teacher')
]
auth_user = models.OneToOneField(User, on_delete=models.CASCADE)
name = models.CharField(max_length=50)
school_content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
school_id = models.PositiveIntegerField()
school = GenericForeignKey('school_content_type', 'school_id')
role = models.CharField(null=True, blank=True, choices=ROLES, max_length=20)
class Places(BaseModel):
name = models.CharField(max_length=50)
code = models.CharField(max_length=12, unique=True)
class Department(BaseModel):
TYPES = [
('ONLINE', 'Online'),
('OFFLINE', 'OfFline')
]
department_type = models.CharField(max_length=15, choices=TYPES)
service_no = models.CharField(max_length=50)
instructions = models.TextField(null=True, blank=True)
place = models.ForeignKey(Places, to_field='code', db_column='place_code', on_delete=models.PROTECT)
SERIALIZERS
class DepartmentCreateSerializer(serializers.ModelSerializer):
place_code=serializers.CharField(read_only=True)
class Meta:
model=Department
fields = ('department_type','service_no','instructions')
def get_place(self, request):
user_id=self.context['request'].user.id
school_id=Staff.objects.get(auth_user_id= user_id).school_id
places_code_name=Places.objects.get(id= school_id).name
class PlacesSerializer(serializers.ModelSerializer):
class Meta:
model = Places
fields = ('id', 'code', 'name')
from places.serializers import PlacesSerializer
class DepartmentCreateSerializer(serializers.ModelSerializer):
place= PlacesSerializer(read_only=True)
class Meta:
model=Department
fields = ('place','service_no','instructions')
def validate(self, attrs):
palce_obj = self.context['request'].user.staff.place()
attrs.update({'place': place_obj})
attrs = super().validate(attrs)
if not attrs.get('place', None):
raise serializers.ValidationError({'place': ["Place required"]})
return attrs

Django Frontend Inline Form with related models

I'm a python/django newbie. Sorry for my english I'm not a native english speaker. I am trying to make a form where users can add in related models. On the admin site I am able to do this however I am having a hard time implementing the same thing on the website end.
Below is the admin screenshot. I want to implement on the front end the same thing where there's a way to add New Artist and New Tags to the form.
http://postimg.org/image/lv83s9fq5/
Here's my models.py
from django.db import models
from uuslug import uuslug
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
class Tag(models.Model):
name = models.CharField(max_length=200, unique=True)
slug = models.CharField(max_length=200, unique=True)
class Meta:
ordering = ["name"]
def __str__(self):
return self.slug
class Artist(models.Model):
name = models.CharField(max_length=100, unique=True)
slug = models.CharField(max_length=100)
class Meta:
ordering = ["name"]
def __str__(self):
return self.name
class Song(models.Model):
S_KEYS = (
('C', 'C'),
('C#', 'C#'),
('D', 'D'),
('D#', 'D#'),
('E', 'E'),
('F', 'F'),
('F#', 'F#'),
('G', 'G'),
('G#', 'G#'),
('A', 'A'),
('A#', 'A#'),
('B', 'B'),
)
title = models.CharField(max_length=200)
artist = models.ForeignKey(Artist)
user = models.ForeignKey(User)
song_key = models.CharField(max_length=2, choices=S_KEYS)
body = models.TextField()
views = models.IntegerField(default=0)
slug = models.CharField(max_length=100, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
tags = models.ManyToManyField(Tag, blank=True)
class Meta:
ordering = ["title"]
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.slug = uuslug(self.title, instance=self, max_length=100)
super(Song, self).save(*args, **kwargs)
def add_view_count(self):
if self.views is not None:
self.views +=1
Here's what's in my views.py
class SongAdd(generic.CreateView):
template_name= 'song/add.html'
model = Song
form_class = SongForm
success_url = '/'
def form_valid(self, form):
form.instance.user = self.request.user
return super(SongAdd, self).form_valid(form)
I hope you can help me :)
Thanks in advance!
Use django-dynamic-formset app, the same that admin use with the name inlines.js. It has good examples for exactly what you need.

Error with Nested Serializer

I have the following model and serializer:
class SeminarTracking(models.Model):
id = models.BigIntegerField(primary_key=True)
id_tax_seminar = models.BigIntegerField(null=True, blank=True)
cid = models.ForeignKey('clients.Client')
invite = models.IntegerField(null=True, blank=True)
invite_2 = models.IntegerField(null=True, blank=True)
rsvp = models.CharField(max_length=255L, blank=True)
attendees = models.IntegerField(null=True, blank=True)
names = models.CharField(max_length=255L, blank=True)
notes = models.TextField(blank=True)
class Meta:
db_table = 'tax_seminars_tracking'
class SeminarTrackingSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = SeminarTracking
Client serialize:
class ClientSerializer(serializers.HyperlinkedModelSerializer):
#adwebsite = serializers.PrimaryKeyRelatedField()
#adissue = serializers.PrimaryKeyRelatedField(required=False)
#category = serializers.PrimaryKeyRelatedField(many=True)
status = serializers.Field()
seminars = SeminarTrackingSerializer(source='seminartracking_set')
class Meta:
model = Client
Whenever I access the client via the API, I get the following error:
(1054, "Unknown column 'tax_seminars_tracking.cid_id' in 'field list'")
If I access seminartracking_set in a view or template, it works no problems. Is there something I am missing? - Note the primary key of the client model is clientid, not id - not sure if that's relevant.
Many thanks, Ben
I had to specify the column name on the "cid" feild.

Resources