I have the following view -
class File_List(generics.ListAPIView):
model = cdx_composites_csv
serializer_class = cdx_compositesSerializer
def get_queryset(self):
"""
This view should return a list of all the purchases for
the user as determined by the username portion of the URL.
"""
filename = self.request.GET.get('filename')
model = get_model('markit', filename)
filedate = self.request.GET.get('filedate')
queryset = model.objects.using('markitdb').filter(Date__contains=filedate)
return queryset
For now ignore that model is listed twice it's the same model I need to figure that out later.
For permissions, I want it to deny access if the following occurs.
I have an extended Auth in my models.py
class FileIndex(models.Model):
filename = models.CharField(max_length=256)
filetype = models.CharField(max_length=16)
vendorid = models.IntegerField()
vendorname = models.CharField(max_length=256)
tablename = models.CharField(max_length=256)
class Meta:
db_table = 'file_index'
verbose_name = 'File/Vendor Index'
verbose_name_plural = 'File/Vendor Indicies'
def __str__(self):
return self.filename
class UserFile(models.Model):
userid = models.ForeignKey(User)
fileid = models.ForeignKey(FileIndex)
grant_date = models.DateTimeField()
revoke_date = models.DateTimeField(blank=True)
class Meta:
db_table = 'auth_files'
verbose_name = 'User File Matrix'
verbose_name_plural = 'User File Matricies'
I want to be able to say that if the filename does not show up in relation to the user access will be denied.
In SQL it's simply a matter of returning the list of filenames the user has access to -
select * from auth_files af
inner join auth_user au on au.id = af.userid
inner join file_index fi on fi.id = af.fileid
where au.user = 'user logged in'
then I want to tell it that if that if the filename in the request is in the list of filenames returned from the above query then yes the user can do what they want to do.
update - tweaking like so -
class File_List(generics.ListAPIView):
model = cdx_composites_csv
serializer_class = cdx_compositesSerializer
def get_queryset(self):
# authorized_files = UserFile.objects.filter(userid=self.request.user).values_list('fileid')
"""
This view should return a list of all the purchases for
the user as determined by the username portion of the URL.
"""
filename = self.request.GET.get('filename')
model = get_model('markit', filename)
filedate = self.request.GET.get('filedate')
if FileIndex.objects.filter(filename=filename).exists():
queryset = model.objects.using('markitdb').filter(Date__contains=filedate)
return queryset
I now get the following error -
'NoneType' object is not iterable
In your view you can check like this:
filename = self.request.GET.get('filename')
if FileIndex.objects.filter(filename=filename).exists():
#do something
Related
I have an app that creates modelForms dynamically based on models. These forms must be dynamically loaded on the template.
I need to write a function in my view that gets all the forms from forms.py or gets a specific form by it's name. Something similar to get_models(appName) and get_model(AppName, modelName) but for forms instead of models.
How can I write it and where should I write it?
in Registery or in forms or somewhere else?
Here is my code:
Models.py
class PrimaryInfo(models.Model):
Name = models.CharField(max_length=200, blank=False, null=True) #required. Must be filled by user
Surname = models.CharField(max_length=200, blank=False, null=True)
DateOfBirth = models.DateField('date of birth', blank=False, null=True)
<Some Other fields>
...
def calculateAge(self):
if not self.DateOfBirth is None:
thisYear = timezone.now().date().year
return thisYear - self.DateOfBirth.year
pass
<some other functions>
...
#Here come all the related tables
class Jobs(models.Model):
Rel = models.ForeignKey(PrimaryInfo, on_delete=models.CASCADE)
Job = models.CharField(max_length=200)
Age = models.IntegerField(default=0)
Country = models.CharField(max_length=200)
def __str__(self):
return self.Job
<some other related models>
....
My View:
def detail(request, personId):
appName = urls.app_name
tablesPrefix = appName + '_'
person = PrimaryInfo.objects.get(pk = personId)
peopledbModels = apps.get_models(appName)
fieldsList = []
relatedModels = []
relationshipsDic = {}
formsFileName = "write.py"
parentModelForms = []
# identify which models are parent and which ones are relationships. makes a dictionary (Of string, list) for the results.
for m in peopledbModels:
if m._meta.db_table.startswith(appName):
fields = m._meta.get_fields()
for fld in fields:
if fld.is_relation:
if fld.many_to_one or fld.many_to_many or fld.one_to_one:
pass
else:
relatedModels.append(fld.name)
relationshipsDic["{}".format(m._meta.label).replace("{}.".format(appName),"")] = relatedModels
#Write the modelForm from parent model into forms.py
for pmdl in relationshipsDic.keys():
parentModelName = pmdl
modelFormExist = False
with open("{}{}/{}".format(djangoSettings.MEDIA_ROOT, appName, formsFileName)) as file:
if "class {}Form(forms.ModelForm):".format(parentModelName) in file.read():
file.close()
modelFormExist = True
if modelFormExist == False:
with open("{}{}/{}".format(djangoSettings.MEDIA_ROOT, appName, formsFileName), "a+") as file:
file.write("\n\nclass {0}Form(forms.ModelForm):\n\tclass Meta:\n\t\tmodel = {0}\n\t\tfields = '__all__'".format(parentModelName))
file.close()
parentModel = apps.get_model(appName, pmdl)
instance = get_object_or_404(parentModel, pk=personId)
parentModelForm = "{}Form".format(pmdl) #this is where I need to get the form object with a variable name from forms.py
parentModelForm = parentModelForm(instance = instance) #this is not working (string object is not callable)
parentModelForms.append(parentModelForm)
<then pass this list of models to the template>
...
My forms (automatically populated from my view):
class PrimaryInfoForm(forms.ModelForm):
class Meta:
model = PrimaryInfo
fields = '__all__'
class JobsForm(forms.ModelForm):
class Meta:
model = Jobs
fields = '__all__'
So I have a problem about how to save model instance with foreign key relation,
models.py
class Connect(models.Model):
username = models.CharField(max_length=255)
password = models.CharField(max_length=255,null=True, blank=True)
conft = models.TextField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.username)
class Ip(models.Model):
class Meta:
db_table = 'autonet_ip'
connect_id = models.ForeignKey(Connect, on_delete=models.CASCADE)
ipaddr = models.CharField(max_length=255)
def __str__ (self):
return self.ipaddr
forms.py
class NacmForm(ModelForm):
password = forms.CharField(widget=forms.PasswordInput,required = False)
class Meta:
model = Connect
fields = ['username', 'password','conft']
labels = {'conft':_('Config'),}
class IpForm(ModelForm):
class Meta:
model = Ip
fields = ['ipaddr']
labels = {'ipaddr':_('IP address'),}
IpFormset = formset_factory(IpForm, extra=1)
views.py
def konfig(request):
ip_list = []
status = ''
value_bak = 1
if request.method == 'POST':
formm = NacmForm(request.POST or None)
ipform = IpFormset(request.POST)
upform = UploadForm(request.POST,request.FILES)
userValue = formm['username'].value()
passValue = formm['password'].value()
confValue = formm['conft'].value()
usernamef = get_object_or_404(Connect, pk=id)
if ipform.is_valid():
for form in ipform:
ipaddr = form.cleaned_data.get('ipaddr')
//.... some code ...//
simpanIp = form.save(commit=False)
simpanIp.connect_id = usernamef
simpanIp.save()
simpanForm.save()
return HttpResponseRedirect('/konfig')
else:
formm = NacmForm()
ipform = IpFormset()
return render(request, 'konfig.html', {'form': formm, 'logins': Connect.objects.all(), 'ipform': ipform, 'status': status })
Then, when I input all data and click submit to collect form data and on simpanIp.save(), I've got an error: id() takes exactly one argument (0 given).
I just want to know how to save the instance of Connect model to database with foreign key, thanks in advance
so i edit my models.py like this
class Connect(models.Model):
......
def get_usernameinf(self):
return ', '.join(self.usernameinf.all().values_list('username', flat=True))
and views.py like this
if request.method == 'POST':
.....some code.....
if ipform.is_valid() and formm.is_valid():
simpanForm = formm.save()
for form in ipform:
simpanIp = form.save(commit=False)
...... some code ..
simpanIp.connect_id = simpanForm
simpanIp.save()
and its work, now the result is my "connect_id" got value from "Connect id"
id is a Python builtin that gives a unique ID for an object. I would guess that you did not intend to pass it get_object_or_404 on this line:
get_object_or_404(Connect, pk=id)
The calling convention for this functions seems to be that it is meant to be an integer for the primary key in a database table. Figure out where you should be getting your primary key from and set it correctly.
Pro-tip: avoid using names that are predefined by Python (see here for a full list). It can lead to headaches like the one you just had.
Error message
Cannot assign "<SimpleLazyObject: <User: dip7777>>": "ImageTarget.uploaderclient" must be a "UploaderClient" instance.
I'm working on Python 3.4.3 and Django 1.8 and using Django Rest Framework 3.
I'm using the DRF browsable API for testing.
What I'm trying to do is to attach the currently logged in user to the file that he uploads.
Whenever I try to do a POST and upload a file with a logged in user, it throws up the above error.
I have a couple of UploaderClient created and the user dip7777 is one such user tied to a UploaderClient. I have logged in using that user.
What I am able to accomplish with the current code is edit the currently uploaded file ie. upload a new file in place of the current file using the ImageTargetDetail view.
(I had uploaded a couple of files and tied them to UploaderClients using the admin interface)
What I want to do is upload a new file using the ImageTargetList view and not replace a preexisting one.
But the error shows up and I do not understand how to assign the current(logged in) uploaderclient instance to the ImageTarget's uploaderclient
I have two models 1)UploaderClient
class UploaderClient(models.Model):
user = models.OneToOneField(User)
company_name = models.CharField(_('company name'), max_length=100)
class Meta:
verbose_name = _('uploaderclient')
verbose_name_plural = _('uploaderclients')
def __str__(self):
return 'UploaderClient: {}'.format(self.user.username)
and 2) ImageTarget
class ImageTarget(models.Model):
uploaderclient = models.ForeignKey('authenticateclients.UploaderClient', related_name='imagetargets')
targetName = models.CharField(max_length=50, unique=True)
imageWidth = models.IntegerField()
targetFile = models.FileField(upload_to=get_upload_imagetargetfile_name)
in two different apps authenticateclients and clientupload
My serializers are:
class UploaderClientSerializer(serializers.HyperlinkedModelSerializer):
username = serializers.CharField(source='user.username')
email = serializers.CharField(source='user.email', required=False, allow_null=True)
password = serializers.CharField(source='user.password', write_only=True, required=False)
date_joined = serializers.DateTimeField(source='user.date_joined', read_only=True)
last_login = serializers.DateTimeField(source='user.last_login', read_only=True)
company_name = serializers.CharField()
imagetargets = serializers.HyperlinkedRelatedField(many=True, view_name='imagetargetsdetail', read_only=True)
url = serializers.HyperlinkedIdentityField(view_name='uploaderclientsdetail')
class Meta:
model = UploaderClient
fields = ('url', 'email', 'username', 'company_name', 'date_joined', 'last_login', 'password','imagetargets',)
read_only_fields = ('user.date_joined', 'user.last_login',)
and
class ImageTargetSerializer(serializers.HyperlinkedModelSerializer):
uploaderclient = UploaderClientSerializer(read_only=True,required=False)
targetFile = serializers.FileField(label='TargetFile')
url = serializers.HyperlinkedIdentityField(view_name='imagetargetsdetail')
class Meta:
model = ImageTarget
fields = ('url','uploaderclient','targetName','imageWidth','targetFile',)
def get_validation_exclusions(self):
exclusions = super(ImageTargetSerializer, self).get_validation_exclusions()
return exclusions + ['uploaderclient']
My views are
class UploaderClientList(generics.ListAPIView):
queryset = UploaderClient.objects.all()
serializer_class = UploaderClientSerializer
class UploaderClientDetail(generics.RetrieveAPIView):
queryset = UploaderClient.objects.all()
serializer_class = UploaderClientSerializer
and
class ImageTargetList(generics.ListCreateAPIView):
queryset = ImageTarget.objects.all()
serializer_class = ImageTargetSerializer
def perform_create(self, serializer):
instance = serializer.save(uploaderclient=self.request.user)
return super(ImageTargetList, self).perform_create(serializer)
permission_classes = (permissions.IsAuthenticated,IsOwnerOrNothing,)
class ImageTargetDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = ImageTarget.objects.all()
serializer_class = ImageTargetSerializer
permission_classes = (permissions.IsAuthenticated,IsOwnerOrNothing,)
#api_view(('GET',))
def api_root(request, format=None):
return Response({
'uploaderclients': reverse('uploaderclients', request=request, format=format),
'imagetargets': reverse('imagetargets', request=request, format=format),
})
In ImageTargetList.perform_create:
instance = serializer.save(uploaderclient=self.request.user)
However uploaderclient should not be a user.
I have the following models.py
class FileIndex(models.Model):
filename = models.CharField(max_length=256)
filetype = models.CharField(max_length=16)
vendorid = models.IntegerField()
vendorname = models.CharField(max_length=256)
tablename = models.CharField(max_length=256)
class Meta:
db_table = 'file_index'
verbose_name = 'File/Vendor Index'
verbose_name_plural = 'File/Vendor Indicies'
def __str__(self):
return self.filename
class UserFile(models.Model):
userid_id = models.ManyToManyField(User)
fileid_id = models.ManyToManyField(FileIndex)
grant_date = models.DateTimeField()
revoke_date = models.DateTimeField(blank=True)
class Meta:
db_table = 'auth_files'
verbose_name = 'User File Matrix'
verbose_name_plural = 'User File Matricies'
def get_userids(self):
return "\n".join([u.pk for u in self.userid_id.all()])
def get_fileids(self):
return "\n".join([f.pk for f in self.fileindex.all()])
I then run the following view in my views.py -
class ExampleView(APIView):
model = cdx_composites_csv
serializer_class = cdx_compositesSerializer
def get(self, request, format=None):
if UserFile.objects.filter(fileid_id=1, userid_id=2).exists():
content = {
'status': 'Request Successful.'
}
return Response(content)
else:
content = {
'status': 'Request Failed.'
}
return Response(content)
It fails with the following error message -
"auth_files_fileid_id" is an index
The SQL it's trying to run is the following -
u'SELECT (1) AS "a" FROM "auth_files"
INNER JOIN "auth_files_fileid_id" ON ( "auth_files"."id" = "auth_files_fileid_id"."userfile_id" )
INNER JOIN "auth_files_userid_id" ON ( "auth_files"."id" = "auth_files_userid_id"."userfile_id" )
WHERE ("auth_files_fileid_id"."fileindex_id" = 1 AND "auth_files_userid_id"."user_id" = 2 ) LIMIT 1'
If I were to write the query in the DB this is how it would look - for some reason the query is waaaaaay off and I'm unsure why it's registering like above.
select * from auth_files af
inner join auth_user on au.id = af.userid
inner join fileindex fi on fi.id = af.fileid
where fi.id = 1 and au.id = 2
Looking at the query that Django is creating it's trying to inner join on itself I have no idea why.
This is ultimately what I want the query to be -
select fi.filename from auth_files af
inner join auth_user on au.id = af.userid
inner join fileindex fi on fi.id = af.fileid
where fi.filename = 'filename' and au.username = 'testuser'
If you want those queries, then you have the wrong relationship type (not to mention your extremely odd field names). You're describing a foreign key relationship, but you have declared a many-to-many. M2M relationships require a linking table, which Django automatically names with the source table + fieldname - but apparently this conflicts with an index you have defined.
I would change those very strange fieldnames into something more standard ("users" and "fileindexes" would do fine) and recreate the database.
Edited As confirmed by the comments, you do have the wrong field type here. UserFile is already the linking table of a many-to-many relationship, so you need to use ForeignKey fields to both User and File. Again, though, you should use sensible names: each UserFile can only relate to one of each, so these names should be "user" and "file".
The many-to-many relationship is really from FileIndex to User, going through FileIndex, so you should declare it like that.
class UserFile(models.Model):
user = models.ForeignKey(User)
file = models.ForeignKey(FileIndex)
...
class FileIndex(models.Model):
users = models.ManyToManyField(User, through=UserFile)
I have a model like this:
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)
display_desc = models.CharField('Description',max_length=500,
blank=True, null=True)
price = models.FloatField()
discountable = models.BooleanField(blank=True, default=False)
image_path = models.CharField(max_length=300,unique=True, blank=True, null=True)
def __unicode__(self):
return self.code + ' : ' + self.desc
But unfortunately, I don't want to store the item's image in the database, instead I want to store the image path in the server in the image_path column.
So, I created a custom admin.py for this object so that I could edit/insert the object thru the Django admin module. As a result, below is the customized admin.py
class ItemAdminForm(forms.ModelForm):
file_upload = forms.FileField(required=False)
class Meta:
model = Item
def __init__(self, *args, **kwargs):
super(ItemAdminForm, self).__init__(*args,**kwargs)
#if kwargs.has_key('instance'):
# instance = kwargs['instance']
# self.initial['file_upload'] = instance.file_upload
def handle_uploaded_file(f):
destination = open('D:/Project/pensel/penselshop/static/picture', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
return f.name
def save(self,commit=True):
name = None
extension = None
#error here! How could I get the request?
miniform = ItemAdminForm(request.POST, request.FILES)
if miniform.is_valid():
name = handle_uploaded_file(request.FILES['file_upload'])
extension = name.split('.')[1]
model = super(ItemAdminForm, self).save(commit=False)
model.image_path = '/static/picture/' + model.code + extension
if commit:
model.save()
return model
However, during processing the save() function, I noticed that there is an error in getting the request. How can I get the request so that I could retrieve the file? I noticed that the request is automatically added in views.py, but not admin.py
Django's ImageField and FileField fields don't actually store the image in the database either. All that is stored in the database is the path, which you can control yourself. The actual image file is stored on the filesystem. So I'm not sure why you are going to all this trouble...?
But to answer your question of how to get the request in the admin, you can override ModelAdmin.save_model().