Below is my models.py.
class Report(models.Model):
company_name = models.CharField(max_length = 40, default = "-")
favorite = models.ManyToManyField(settings.AUTH_USER_MODEL,
through='Fav', related_name='favorite_reports')
def __str__(self):
return self.company_name
class Fav(models.Model) :
report = models.ForeignKey(Report, on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
related_name='favs_users')
class Meta:
unique_together = ('report', 'user')
def __str__(self) :
return '%s likes %s'%(self.user.username, self.report.company_name[:10])
Below is views.py
def post(self, request, pk) :
t = Report.objects.get(id = pk)
fav = Fav(user=request.user, report=t)
try:
fav.save() # In case of duplicate key
print(request.user.favorite_reports) #home.Report.None
print('t =', t.favorite) # t = auth.User.None
print('fav =',fav.report, fav.user) # fav = untitle hello1
print('uesr =',request.user, request.user.favorite_reports,request.user.favs_users) #uesr = hello1 home.Report.None home.Fav.None
except IntegrityError as e:
pass
return redirect('home:homepage')
I want to show different button color to user whether user fav some report or not. So I checked request.user.favorite_reports and request.user.favs_users after I save Fav but it return home.Report.None home.Fav.None
But When I print fav.report, fav.user It returns well. Why this happened?
How can I check user Fav some report in template?
like {% if report.company_name in user.favorite_reports %} But It dosen't works.
You should call request.user.favorite_reports.all()
Related
I don't know why the error is caused even though I designated the author.
I'd appreciate your help.
model & form
class SuperTitle(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='debate_author')
super_title = models.CharField(max_length=100)
liker = models.ManyToManyField(User, related_name='debate_liker')
def __str__(self):
return self.super_title
class SuptitForm(forms.ModelForm):
class Meta:
model = SuperTitle
fields = ['super_title']
views.py
def create(request):
...
dic = {'super_title' : request.POST.get('sup_title')}
sup_form = SuptitForm(dic)
if sup_form.is_valid():
sup_form.author = request.user
sup_form.super_title = ...
sup_form.save()
...
return
IntegrityError at /polls/debate/create/
NOT NULL constraint failed: polls_supertitle.author_id
You get this error becouse you want to save form(create new SuperTitle objetd in database) without author.
You need to pass author objectd or id somehow to form. If the request.user is author i recommend form:
class SuptitForm(forms.ModelForm):
class Meta:
model = SuperTitle
fields = ['super_title', 'author']
widgets = {'author': forms.HiddenInput()}
and in view:
def create(request):
if request.method == 'POST':
sup_form = SuptitForm(request.POST)
if sup_form.is_valid():
sup_form.save()
else:
sup_form = SuptitForm(initial={'author': request.user}
...
I want to automatically generate the "path" field of my model when I use the create method of my serializer. Otherwise I would like to be able to pass this field in my request to be able to modify it later.
I haven't found a way to make a single field of my template optional.
Here is my model :
models.py
class Shop(models.Model):
name = models.CharField(max_length=255)
category = models.ForeignKey(ShopCategory, on_delete=models.SET_NULL, null=True, blank=True)
description = models.TextField(blank=True, null=True)
path = models.CharField(max_length=255, unique=True)
mustBeLogged = models.BooleanField(default=False)
deliveries = models.FloatField(default=7)
def __str__(self):
return self.name
Here is my code :
serializer.py
class ShopSerializer(serializers.ModelSerializer):
class Meta:
model = Shop
exclude = ['path']
def create(self, validated_data):
path = validated_data["name"].replace(" ", "-").lower()
path = unidecode.unidecode(path)
unique = False
while unique == False:
if len(Shop.objects.filter(path=path)) == 0:
unique = True
else:
# Generate a random string
char = "abcdefghijklmnopqrstuvwxyz"
path += "-{}".format("".join(random.sample(char, 5)))
shop = Shop.objects.create(**validated_data, path=path)
shop.save()
return shop
views.py
def post(self, request):
"""For admin to create shop"""
serializer = ShopSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors)
You can set path to be accepted as Null and pop it in create method. In this way you won't have to add it in exclude.
class ShopSerializer(serializers.ModelSerializer):
path = serializers.CharField(allow_blank=True, allow_null=True)
class Meta:
model = Shop
fields = '__all__'
def create(self, validated_data):
validated_data.pop('path')
path = validated_data["name"].replace(" ", "-").lower()
path = unidecode.unidecode(path)
unique = False
while unique == False:
if len(Shop.objects.filter(path=path)) == 0:
unique = True
else:
# Generate a random string
char = "abcdefghijklmnopqrstuvwxyz"
path += "-{}".format("".join(random.sample(char, 5)))
shop = Shop.objects.create(**validated_data, path=path)
shop.save()
return shop
def update(self, instance, validated_data):
#You will have path in validated_data
#And you may have to check if the values are null
return super(ShopSerializer, self).update(instance, validated_data)
Apart from this, in your code:
while unique == False:
if len(Shop.objects.filter(path=path)) == 0:
This would be very costly operation. In every loop it would fire a query, I'm not sure about the logic behind it but you may want to change this.
I am getting FieldError as :
Unknown field(s) (notedate) specified for AssistantNotes
When i call the page. It throws this error. I am using Django 1.9.5 and python 2.7.
I have notedate field in the AssistantNotes table in my db. If i delete "notedate" from modelformset_factory row in my view, it works. I couldnt solve why it is not showing notedate although it is in DB and in model. And generating error. The field is already in the model.
My view is :
def edit_assistant_notes(request):
isassistantsuperadmin = getUserPermissions(request) #Yes if 1, no if 0
list = getUserType(request)
userisassistant = list[2]
if userisassistant == "YES" or isassistantsuperadmin ==1:
list = getUserType(request)
type = list[0]
usertype = list[1] #"Nöbetçi Muavin":1 , "Yetkili":2
if request.method == 'GET':
if AssistantNotes.objects.filter(notedate=nicosia_date(datetime.today()).date()).count() == 0:
AssistantNotesFormsetFactory = modelformset_factory(AssistantNotes, fields=('time', 'notedate', 'categories', 'type', 'dailynote',))
else:
AssistantNotesFormsetFactory = modelformset_factory(AssistantNotes, fields=('time', 'notedate', 'categories', 'type', 'dailynote',), can_delete=True)
if usertype == 1:
formset = AssistantNotesFormsetFactory(queryset=AssistantNotes.objects.filter(notedate=nicosia_date(datetime.today()).date(), type=type))
elif usertype == 2:
formset = AssistantNotesFormsetFactory(queryset=AssistantNotes.objects.all().order_by("notedate", "time"))
helper = TableInlineHelper()
return render(request, 'edit-assistant-notes.html', {'formset': formset, 'helper': helper})
My model is :
class AssistantNotes(BaseModel):
categories = models.CharField(choices=CATEGORIES, default="GENERAL", max_length=100, verbose_name=_("CAT"))
time = models.CharField(choices=TIME, default="-------------", max_length=20, verbose_name=_("Time"))
dailynote = models.TextField(null=True, blank=True, verbose_name=_("Add Note"))
writer = models.TextField(null=True, blank=True, verbose_name=_("Adder"))
notedate = models.DateField(auto_now_add=True, db_index=True, verbose_name=_("Date"))
type = models.CharField(choices=SCHOOLTYPE, default="---", max_length=100, verbose_name=_("SchoolType"))
def __unicode__(self):
return "%s / %s" % (self.dailynote, self.categories)
class Meta:
ordering = ['dailynote']
How can i force this field to be editable ?
Instead of setting auto_now_add, you may override the save() method of the model, say
class AssistantNotes(BaseModel):
....
notedate = models.DateField(db_index=True, verbose_name=_("Date"))
def save(self, *args, **kwargs):
if not self.id:
self.notedate = timezone.now()
return super(AssistantNotes, self).save(*args, **kwargs)
I'm trying to map a foreign key to POST data when creating a new object through a serializer. There are two foreign keys in the object, one is serializing perfectly, the other is creating an error.
Model:
class Event(models.Model):
owner = models.ForeignKey('auth.User', related_name='owner', blank=True)
date = models.DateField('eventdate')
time = models.TimeField('eventtime', default=now)
eventtype = models.ForeignKey(EventType, related_name='eventtype', blank=True)
# duration = models.DurationField()
location = models.CharField(max_length=200, blank=True)
attenders = models.ManyToManyField(User, related_name='attenders')
invited = models.ManyToManyField(User, related_name='invitedlist')
View:
class EventMixin(RetrieveUpdateDestroyAPIView, CreateAPIView):
serializer_class = EventSerializer
def get_queryset(self):
return Event.objects.all()
def partial_update(self, request, *args, **kwargs):
request['owner'] = request.user
sname = request['eventtype']
request['eventtype'] = EventType.objects.filter(sname=sname)
json_str = json.dumps(self.request.data)
data = json.loads(json_str)
try:
invited = list(data['toInvite'])
for i in invited:
for j in User.objects.filter(username=i):
invite = EventInvite(invited=j, sender=request.user, event=self.get_object())
invite.save()
self.get_object().invited.add()
except KeyError:
pass
return super(EventMixin, self).partial_update(request, *args, **kwargs)
def create(self, request, *args, **kwargs):
new = {}
new['owner'] = request.user.__dict__
new['date'] = request.data['date']
new['time'] = request.data['time']
new['location'] = request.data['location']
sname = request.data['eventtype']
new['eventtype'] = EventType.objects.get(sname=sname).__dict__
json_str = json.dumps(self.request.data)
data = json.loads(json_str)
serializer = EventMixinSerializer(data=new)
with open('/tmp/log.txt', 'w+') as f:
f.write(str(serializer.is_valid()))
f.write(str(serializer.validated_data))
f.close()
serializer.is_valid();
serializer.save()
try:
invited = list(data['toInvite'])
for i in invited:
for j in User.objects.filter(username=i):
invite = EventInvite(invited=j, sender=request.user, event=self.get_object())
invite.save()
self.get_object().invited.add()
except KeyError:
pass
Serializer:
class EventMixinSerializer(serializers.ModelSerializer):
owner = UserSerializer(read_only=True)
eventtype = EventTypeSerializer()
attenders = FriendsListingField(read_only=True)
invited = FriendsListingField(read_only=True)
class Meta:
model = Event
fields = ('owner', 'eventtype', 'date', 'time', 'location', 'id', 'attenders', 'invited')
def create(self, validated_data):
owner = validated_data.pop('owner')
owner = owner.instance
eventtype = validated_data.pop('eventtype')
eventtype = eventtype.instance
event = Event.objects.create(owner=owner, eventtype=eventtype, **validated_data)
event.save()
return event
Error when owner field present:
False
{'owner': OrderedDict([('username', ['A user with that username already exists.'])])}
Result when UserSerializer(read_only=True) (pretty much diabling it):
True
OrderedDict([('eventtype', OrderedDict([('lname', 'Swimming'), ('sname', 'SWM'), ('category', '1')])), ('date', datetime.date(2015, 12, 22)), ('$
(Notice the event type data in the result)
Thanks!
You need to remove the validators from UserSerializer.
Assuming UserSerializer is a User ModelSerializer it'll extract the unique constraint on the User.username from the Model and your validation will fail.
To work this around you'll need to remove the UniqueValidator by overriding the validators list for the username field of the UserSerializer
I faced the error when I tried to capture the POST data from a form. Weird, because the same algorithm works with another django app model.
The models:
class Item(models.Model):
code = models.CharField(max_length=200, unique=True)
barcode = models.CharField(max_length=300)
desc = models.CharField('Description',max_length=500)
reg_date = models.DateField('registered date')
registrar = models.CharField(max_length=100)
def __unicode__(self):
return self.code + ' : ' + self.desc
class ItemInfo(models.Model):
model = models.ForeignKey(Item)
supplier = models.ForeignKey(Supplier)
stock_on_hand = models.IntegerField()
stock_on_order = models.IntegerField()
cost = models.IntegerField()
price = models.IntegerField()
unit = models.CharField(max_length=100)
lead_time = models.IntegerField()
def __unicode__(self):
return Item.code + ' : ' + supplier
class ItemForm(ModelForm):
class Meta:
model = Item
class ItemInfoForm(ModelForm):
class Meta:
model = ItemInfo
exclude = ('model')
And the views.py function for non-working (Item) is like this:
def register(request):
csrf_context = RequestContext(request)
current_user = User
if request.user.is_authenticated():
if request.POST:
item = Item()
item_info = ItemInfo()
header_form == ItemForm(data=request.POST,instance=item)
details_form == ItemInfoForm(data=request.POST, instance=item_info)
if header_form.is_valid():
header = header_form.save()
if details_form.is_valid():
details = details_form.save(commit=False)
details.supplier = header
details.save()
return HttpResponseRedirect('/item/')
else:
return render_to_response('error/denied_data_entry.html')
else:
header_form = ItemForm()
details_form = ItemInfoForm()
return render_to_response('item/register.html',{'header_form' : header_form, 'details_form' : details_form}, csrf_context)
else:
return render_to_response('error/requires_login.html', csrf_context)
The working views.py function for another working (Supplier) model is here:
def register(request):
csrf_context = RequestContext(request)
current_user = User
if request.user.is_authenticated():
if request.POST:
supplier = Supplier()
supplier_info = SupplierInfo()
header_form = SupplierForm(data=request.POST, instance=supplier)
details_form = SupplierInfoForm(data=request.POST, instance=supplier_info)
if header_form.is_valid():
header = header_form.save()
if details_form.is_valid():
details = details_form.save(commit=False)
details.model = header
details.save()
return HttpResponseRedirect('/supplier/')
else:
return render_to_response('error/denied_data_entry.html')
else:
return render_to_response('error/denied_data_entry.html')
else:
header_form = SupplierForm()
details_form = SupplierInfoForm()
return render_to_response('supplier/register.html', {'header_form' : header_form, 'details_form' : details_form}, csrf_context)
else:
return render_to_response('error/requires_login.html', csrf_context)
The traceback page shows that the POST did pass some variable. Help me please, I cant figure it out why it works on Supplier, but not Item.
P/S: Sorry for the indentation.
The problem is here:
# ...
header_form == ItemForm(data=request.POST,instance=item)
details_form == ItemInfoForm(data=request.POST, instance=item_info)
You're not assigning, you're comparing.