This is my models.py
class PdfParsed(models.Model):
user=models.ForeignKey(User, on_delete=models.CASCADE)
pdf_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200, blank=True, null=True)
status = models.IntegerField(blank=True, null=True)
this is my views.py
class pdfListView(LoginRequiredMixin,ListView):
model = PdfParsed.objects.filter(user_id=6)
login_url = '/login/'
context_object_name = 'pdfparsed_list'
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
aa=os.listdir('media/pdfs')
data['pdf_parsed'] = sum('.pdf' in s for s in aa)
return data
what i wanted to achieve from above code is to print pdfs that are uploaded with respect to user here for testing purpose i used number 6 but its not working it is showing this error:
File "/home/ideas/anaconda3/lib/python3.8/site-packages/django/views/generic/list.py", line 33, in get_queryset
queryset = self.model._default_manager.all()
AttributeError: 'QuerySet' object has no attribute '_default_manager'
add this in your code:
def get_queryset(self):
return super().get_queryset().filter(user_id=self.request.user.id)
and keep model = PdfParsed
Related
I am using django-import-export to import an excel to my model, what I do is that I create a form with some inputs from where it loads the file, then in form_valid() I process the file to load it to the database, the model has two foreign keys 'id_order' and 'gestion'; 'id_orden' comes in the excel and 'gestion' I get it with gestion= Gestion.objects.get(idgestion=obj.pk) which is the id of the form that I am saving, but what I want to know is how I can pass 'gestion' to ModelResource and then save it to the database
view.py
class GestionView(CreateView):
model = Gestion
form_class = GestionForm
template_name = 'asignacion/gestion.html'
success_url = reverse_lazy('asignacion_url:gestion')
def form_valid(self, form):
isvalid = super().form_valid(form)
obj = form.save()
gestion= Gestion.objects.get(idgestion=obj.pk)
file = self.request.FILES['file']
item_gestion =ItemResourceResource()
dataset = Dataset()
imported_data = dataset.load(file.read(), format='xls')
result = item_gestion.import_data(dataset, dry_run=True)
if not result.has_errors():
item_gestion.import_data(dataset, dry_run=False)
model.py
class ItemGestion(models.Model):
idgestion = models.AutoField(primary_key=True)
numero_imagenes = models.CharField(max_length=45, blank=True, null=True)
id_orden = models.ForeignKey('Asignacion', models.DO_NOTHING)
aviso_sap = models.CharField(max_length=45, blank=True, null=True)
poliza = models.CharField(max_length=45, blank=True, null=True)
observacion_cierre = models.CharField(max_length=250, blank=True, null=True)
gestion=models.ForeignKey('Gestion', models.DO_NOTHING)
resources.py
class ItemResourceResource(resources.ModelResource):
id_orden = fields.Field(column_name='id_orden', attribute='id_orden',
widget=ForeignKeyWidget(Asignacion,'id_orden'))
class Meta:
model = ItemGestion
import_id_fields = ('id_orden',)
exclude = ('idgestion', )
It is easy to do. You need to pass the gestion value into your Resource, and then link it to the instance before it is persisted:
class ItemResourceResource(ModelResource):
def __init__(self, gestion):
self.gestion = gestion
def before_save_instance(self, instance, using_transactions, dry_run):
instance.gestion = self.gestion
class Meta:
# ...
gestion = Gestion.objects.get(idgestion=obj.pk)
item_gestion = ItemResourceResource(gestion)
Obviously this means that all the instances created from the rows in your dataset will be linked to the same 'gestion' value.
btw import-export integrates with django-admin, so you can use the admin interface to import data rather than writing your own forms (if that fits your requirements). See the docs for more information.
I would like to create my own endpoint for POST request to two related tables. I have two tables User and Userattribute.
models.py
class User(models.Model):
email = models.CharField(unique=True, max_length=180)
roles = models.JSONField(default=dict)
password = models.CharField(max_length=255, blank=True, null=True)
name = models.CharField(max_length=255, blank=True, null=True)
firebase_id = models.CharField(max_length=255, blank=True, null=True)
created_at = models.DateTimeField(default=now)
progress_sub_step = models.IntegerField(blank=True, null=True)
step_available_date = models.DateTimeField(blank=True, null=True)
progress_step = models.IntegerField(blank=True, null=True)
active = models.IntegerField(default=1)
last_login_at = models.DateTimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'user'
class Userattribute(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name = 'attribute')
attribute = models.ForeignKey(Attribute, on_delete=models.CASCADE)
The table Userattribute contains the field user which is OnetoOne to Id primary key from User table.
I tried to implement POST to two tables in serializers.py In the commented section there is a create definition which works perfectly for me. However, I wouldlike to move it to views.py as register_in_course endpoint
serializers.py
class FilmSerializer(serializers.ModelSerializer):
class Meta:
model = Film
fields = ['tytul', 'opis', 'po_premierze']
class UserattributeSerializer(serializers.ModelSerializer):
class Meta:
model = Userattribute
fields = ['user', 'attribute']
class UASerializer(serializers.ModelSerializer):
class Meta:
model = Userattribute
fields = ['attribute']
class UserSerializer(serializers.ModelSerializer):
attribute = UASerializer(many = False)
class Meta:
model = User
fields = ['email', 'name', 'firebase_id', 'attribute']
# This is what workks perfectly for me, and I want to move it to views.py
# VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
# def create(self, validated_data):
# attribute_data = validated_data.pop('attribute')
# user = User.objects.create(**validated_data)
# Userattribute.objects.create(user=user, **attribute_data)
# return user
Current views.py:
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
#action(detail = False, methods = ['post'])
def register_in_course(self, request, **kwargs):
data = self.get_object()
user = User.objects.create(email=request.data['email'],
name=request.data['name'],
firebase_id=request.data['firebase_id'])
user_id = User.objects.filter(firebase_id = request.data['firebase_id'])['id']
attribute = Userattribute.objects.create(user = user_id, attribute = request.data['attribute']['attribute'])
user = user.attribute.add(attribute)
serializer = UserSerializer(user, many = false)
return Response(serializer.data)
Using endpoint register_in_course to POST I get following error:
Expected view UserViewSet to be called with a URL keyword argument named "pk". Fix your URL conf, or set the .lookup_field attribute on the view correctly.
urls.py
from django.urls import include, path
from django.conf.urls import url
from rest_framework import routers
from api import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'userattribute', views.UserattributeViewSet)
urlpatterns = [
url('', include(router.urls))
]
i removed one line user_id variable and changed attribute variable. please check, maybe it should solve your problem, because you have already have Assigned variable as a User object..
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
#action(detail = False, methods = ['post'])
def register_in_course(self, request, **kwargs):
data = self.get_object()
user = User.objects.create(email=request.data['email'],
name=request.data['name'],
firebase_id=request.data['firebase_id'])
attribute = Userattribute.objects.create(user = user, attribute = request.data['attribute']['attribute']) # changed this line
user = user.attribute.add(attribute)
serializer = UserSerializer(user, many = false)
return Response(serializer.data)
This issue is caused by calling get_object in a view that is defined with detail=False:
#action(detail = False, methods = ['post'])
def register_in_course(self, request, **kwargs):
data = self.get_object() # The problem is caused by this line
It seems you don't need this data, as you are using request.data.
So you can define your view like this:
#action(detail = False, methods = ['post'])
def register_in_course(self, request, **kwargs):
user = User.objects.create(
email=request.data['email'],
name=request.data['name'],
firebase_id=request.data['firebase_id']
)
Userattribute.objects.create(
user=user,
attribute = request.data.get('attribute', {}).get('attribute', {})
)
return Response(UserSerializer(user).data)
For my website, I want users to be able to add tags to their posts. But I get this error:
python name 'Tag' is not defined
Here is some code
Relevant code in models.py
from taggit.managers import TaggableManager
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=75)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
image = models.ImageField(upload_to='post_images',blank=True,null=True)
published_date = models.DateTimeField(blank=True,null=True,auto_now_add=True)
NSFW = models.BooleanField(default=False)
spoiler = models.BooleanField(default=False)
tags = TaggableManager()
def __str__(self):
return self.title
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
Here is the relevant code in views.py
class TagMixin(object):
def get_context_data(self,**kwargs):
context = super(TagMixin,self).get_context_data(**kwargs)
context['tags'] = Tag.objects.all()
return context
class PostListView(TagMixin,ListView):
template_name = 'mainapp/post_list.html'
model = Post
context_object_name = 'posts'
queryset = Post.objects.all()
def get_queryset(self):
return Post.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
class TagIndexView(TagMixin,ListView):
template_name = 'mainapp/post_list.html'
context_object_name = 'posts'
model = Post
def get_queryset(self):
return Posts.objects.filter(tags__slug=self.kwargs.get('slug'))
And here is the form.
class PostForm(forms.ModelForm):
class Meta():
model = Post
fields = ['title','text','image','tags','spoiler','NSFW']
widgets = {
'title':forms.TextInput(attrs={'class':'textinputclass'}),
'text':forms.Textarea(attrs={'class':'textareaclass editable'}),
}
def __init__(self, *args, **kwargs):
super(PostForm, self).__init__(*args, **kwargs)
self.fields['image'].required = False
I am getting the error in the Mixin, on this line python context['tags'] = Tag.objects.all()
Can anyone tell me why I am getting an error of python name 'Tag' is not defined
So far I have changed the casing of the word, I have changed the name, but none of it works.
Thank you for any help you can give :)
I'm following the Django Rest documentation for writing nested serializer but it is giving me attribute error.
Here are my models:
class Objects(TimeStampModel):
projects = models.ForeignKey(Projects,related_name='proj_obj',on_delete=models.CASCADE)
object_name = models.CharField(max_length=50)
object_description = models.TextField()
object_main_table = models.CharField(max_length=50)
object_primary_key = models.CharField(max_length=50)
object_age_field = models.CharField(max_length=50)
date_format = models.CharField(max_length=50)
def __str__(self):
return self.object_name
class ObjectDefinition(TimeStampModel):
ATTRIBUTE = 'Attribute'
RELATION = 'Relation'
TYPE_CHOICES = (
(ATTRIBUTE, 'Attribute'),
(RELATION, 'Relation'),
)
obj = models.ForeignKey(Objects,related_name='obj_def',on_delete=models.CASCADE)
from_table = models.CharField(max_length=50)
from_table_field = models.CharField(max_length=50)
to_table = models.CharField(max_length=50)
to_table_field = models.CharField(max_length=50)
relation_type = models.CharField(max_length=50,choices=TYPE_CHOICES)
relation_sequence = models.CharField(max_length=50)
value_field = models.CharField(max_length=50, blank=True, null=True)
Here is my serializers.py snippet:
class ObjectDefinitionSerializer(serializers.ModelSerializer):
class Meta:
model = ObjectDefinition
fields = ('from_table','from_table_field','to_table','to_table_field','relation_type','value_field')
class ObjectSerializer(serializers.ModelSerializer):
definition = ObjectDefinitionSerializer(many=True)
object_description = serializers.CharField(required=False, allow_null=True, allow_blank=True
)
class Meta:
model = Objects
fields = ('projects','object_name','object_description','object_main_table','object_primary_key','object_age_field','date_format','definition')
def validate(self, data, *args, **kwargs):
date_format = data.get('date_format')
if date_format not in ['YYYYMMDD', 'DDMMYYYY']:
msg = ('Date format is incorrect')
raise serializers.ValidationError({'error_msg': msg})
return super(ObjectSerializer, self).validate(data, *args, **kwargs)
def create(self, validated_data):
definition_data = validated_data.pop('definition')
obj = Objects.objects.create(**validated_data)
for data in definition_data:
ObjectDefinition.objects.create(obj=obj, **data)
return obj
My views.py:
class CreateObject(CreateAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = ObjectSerializer
After hitting POST, objects.create works fine for both the models but at return obj, it throws me this error:
Exception Value:
Got AttributeError when attempting to get a value for field definition on serializer ObjectSerializer.
The serializer field might be named incorrectly and not match any attribute or key on the Objects instance.
Original exception text was: 'Objects' object has no attribute 'definition'.
What am I missing?
The ObjectDefinition.obj's related_name is obj_def which doesn't match your serializer.
You can fix that by providing the source argument:
definition = ObjectDefinitionSerializer(source='obj_def', many=True)
I have app in Django 1.8 and I want to take last object (based on pub_date) and set for this object filed is_mainteaser on True and rest ssould be set on False.
Here is my code, but latest object hasn't field set to True.
class ArticleListView(ListView):
model = Article
queryset = Article.objects.order_by('-pub_date')
def get_context_data(self, **kwargs):
context = super(ArticleListView, self).get_context_data(**kwargs)
lates_object = Article.objects.latest('pub_date')
lates_object.is_mainteaser = True
return context
Here is my model:
class Article(model.Models):
title = models.CharField(max_length=255)
short_text = models.TextField(max_length=10000, default='')
image = FilerImageField(null=True)
pub_date = models.DateTimeField('date published')
online_from = models.DateTimeField('online from', blank=True)
online_to = models.DateTimeField('online to', blank=True)
position = models.PositiveIntegerField(default=0)
is_mainteaser = models.BooleanField(default=False)
def __str__(self):
return self.title
class Meta:
ordering = ['position']
When you have object instance and change model attribute you must save instance. Example:
lates_object = Article.objects.latest('pub_date')
lates_object.is_mainteaser = True
lates_object.save()
I think better for this solution is use django signals or action when you add new article. In ListView is't good solution to do it that.