These are chat archives for django/django

18th
Jul 2018
Curtis Maloney
@funkybob
Jul 18 2018 01:08 UTC
have your view set the created_by to request.user [also, default of '' is not valid for a fkey field]
Lakshminp
@badri
Jul 18 2018 01:58 UTC
@Nkirui on a related note, if you are adding the above fields, you could consider using https://github.com/audiolion/django-behaviors/ for your app(s), avoids writing a lot of boilerplate.
matrixbot
@matrixbot
Jul 18 2018 07:21 UTC
mak1799 Hello. Can anyone help me? I need to synchronize all messages in room, that was created by matrix python sdk but I don't know how to do it. Help, please.
Nathan Kirui
@Nkirui
Jul 18 2018 07:44 UTC
@funkybob and @badri guess i am almost there implemented this on the forms
class DriverForm(ModelForm):
class Meta:
model = DriverProfile
exclude = ['created_by']
    def save_model(self, request, obj,change):
        if change:
            obj.created_by = request.user
        obj.save()
and my views:
def drivercreate(request):
form = DriverForm(request.POST or None)
if form.is_valid():
form.save(request.user)
return redirect('driverlist')
return render(request, 'employer/drivercreate.html', {'form': form})
I used the, exclude = ['created_by'] because i dont want it to be part of the form to be filled ..
MilanRgm
@MilanRgm
Jul 18 2018 13:19 UTC
When uploading image from client to server, what format should be sent? blob file or what format? I am using ImageField
MilanRgm
@MilanRgm
Jul 18 2018 13:35 UTC
class ShopAPI(views.APIView):

    serializer_class = ShopSerializer
    parser_classes = (parsers.MultiPartParser, parsers.FormParser)

    def get(self, request, slug=None):
        reply = {}
        agent = Agent.objects.filter(owner=request.user).first()
        try:
            shops = Shop.objects.filter(agent=agent)
            if slug:
                shop = shops.get(slug=slug)
                reply['data'] = self.serializer_class(shop).data
            reply['data'] = self.serializer_class(shops, many=True).data
        except Shop.DoesNotExist:
            raise serializers.ValidationError('Shop Does not exist')
        else:
            return Response(reply, status.HTTP_200_OK)

    def post(self, request, slug=None):
        shop = None
        agent = Agent.objects.filter(owner=request.user).first()
        if slug is not None:
            try:
                shop = Shop.objects.filter(agent=agent).get(slug=slug)
            except Shop.DoesNotExist:
                raise serializers.ValidationError('Shop Does Not Exist')
        serialized_data = self.serializer_class(
            instance=shop, data=request.data, partial=True)
        reply = {}
        if not serialized_data.is_valid():
            print('error', serialized_data.errors)
            raise serializers.ValidationError(
                serialized_data.errors).as_response()
        shop = serialized_data.save(agent=agent)
        reply['data'] = self.serializer_class(instance=shop, many=False).data

        return Response(reply, status.HTTP_200_OK)
posting from insomnia works but from client side it does not work. It says unsupported media type application/json
if i use content-type as multipart/form-data then i get status 200 but the empty data gets posted
garynobles
@garynobles
Jul 18 2018 13:44 UTC
I have the following model, above this is another model with photobatch_id, they are related through the foreign key, I would like the photos to be uploaded to a folder named the same as the 'photobatch_id' in the photo_number = models.ImageField(upload_to='images/photobatches/photobatch_id/') anyone know how this can be achieved? How do I add some kind of variable - I assume in the view - so the model knows which folder to make/use?
class PhotobatchPhotos(models.Model):
    photo_id = models.AutoField(primary_key=True)
    photobatch_id = models.ForeignKey(Photobatch, db_column='id', on_delete = models.PROTECT, null=True, blank=True)
    photo_number = models.ImageField(upload_to='images/photobatches/photobatch_id/')
    upload_timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(photobatch_id)

    class Meta():
        managed=False
        db_table = 'excavation\".\"photobatch_photos'