Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django Import-Export Importing with User object failing
I am attempting to import data using the Import-Export library. I have a User foreign key in my model called creator, and rather than referencing the pk of the User i want to use usernames. But when i try to import the data it fails Some other Stack posts have suggested adding a skip feature, or creating a custom widget that queries the database first but that is also not working. model class MediaEntry(models.Model): media_number = models.CharField(max_length=20, default=increment, editable=False) media_designation = models.ForeignKey(MediaDesignation, null=True, on_delete=models.SET_NULL) creator = models.ForeignKey(User, on_delete=models.CASCADE) date_used_or_received = models.DateTimeField(default=timezone.now) resources from import_export import resources, fields, widgets from import_export.widgets import ForeignKeyWidget from .models import BlankMedia, MediaLocations, AuditLocation, MediaType, MediaEntry from django.contrib.auth.models import User class MediaEntryResource(resources.ModelResource): creator = fields.Field( column_name='creator', attribute='creator', widget=ForeignKeyWidget(model=User, field='username') ) class Meta: model = MediaEntry fields = ('id', 'media_number', 'creator',) Admin from .resources import BlankMediaResource, MediaLocationResource, MediaTypeResource, AuditLocationResource,MediaEntryResource class MediaEntryAdmin(ImportExportModelAdmin): resource_class = MediaEntryResource If the code worked it should preview the imported values but it fails with NOT NULL constraint failed: medialog_mediaentry.creator_id -
How to save queryset directly to redis?
I'd like to save results of Django QuerySets directly into redis cache instead of using Django's built in cache. The reason is to use a separate redis db for certain queries, in order to accelerate data retrieval. Here is my sample code: def cached_post(id): r = redis.StrictRedis(unix_socket_path='/tmp/redis.sock', db=6) key = "post:" + id if not r.get(key): post = Post.objects.get(pk = id) r.set(key, post, AN_HOUR) return r.get(key) But I get this error: cannot concatenate 'str' and 'long' objects So clearly simple string is not the correct data type to use to store querysets but I don't know what redis data type should I use and how? I have check the docs but not seen any such example, or hints about the data type which django cache.setuses to save querysets. -
Django add unique constraint only for new records
I have a django model: from django.db import models class TestModel(models.Model): name = models.CharField("Name", null=False, blank=False, max_length=300) After some time I got a task to add a unique constraint such that my new model looks like this: from django.db import models class Test(models.Model): name = models.CharField("Title", null=False, blank=False, max_length=300, unique=True) After this I need to do makemigrations and migrate. However in my DB I already have records with duplicate names. My question: I want to apply constraint only for the new records. Is there a way to do so? (allow old duplicates to remain in the DB but prevent new ones to be created). At the moment I am getting IntegrityError on my old records while migrate. -
Django migration didn't migrate authtoken and sessions
When running python manage.py migrate not all migrations were run, specifically django_celery_results, authtoken and sessions. This resulted in the application related migrations erroring out. However, if I first manually migrate those three, and then specifically migrate auth (not sure why I'd need to migrate that again) and then do python manage.py migrate it'll work. The installed apps on Django are like so: INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'rest_framework.authtoken', 'django_celery_results', 'celery.contrib.testing.tasks', 'api_app' ] I'm wondering why that's happening, I thought migrate will run all the migrations listed in "operations to perform". -
Get Timezone to take effect
I am going through the Django tutorial. I thought the TIME_ZONE in settings.py was of form 'UTC-5', but it isn't. I replaced it with 'America/Chicago' However, when I do: python manage.py shell from django.utils import timezone timezone.now() I get 'UTC' How do I get the timezone to take effect? -
data migration is triggering a query at import time
my last migration was a data migration that Trigger thumbnails update for profile app def trigger_thumbnails_update(apps, schema_editor): """ Trigger thumbnails update for profile app """ User = apps.get_model('profile', 'User') for user in User.objects.all(): if user.photo: make_image_thumbnail.delay() class Migration(migrations.Migration): dependencies = [ ('profile', '0008_auto_20190611_2120'), ] operations = [ migrations.RunPython(trigger_thumbnails_update), ] after that, I added a field called is_daleted, and run makemigrations : class Migration(migrations.Migration): dependencies = [ ('profile', '0009_trigger_thumbnails_update'), ] operations = [ migrations.AddField( model_name='user', name='is_deleted', field=models.BooleanField(default=False), ), ] that works fine, but when I run tests (pytest) I get that error : django.db.utils.ProgrammingError: column profile_user.is_deleted does not exist I think that it's caused by my data migration is triggering a query at import time, so it runs before the migration itself. commenting the triggering code solve the problem temporary, I need a real solution, please -
Get several unique fields values in django
I have a model: class MyModel(models.Model): a = models.CharField(...) b = models.CharField(...) And some substring "substring" To get all unique values from fields a and b which contains those substring I can do that: a_values = MyModel.objects.filter(a__icontains=substring).values_list("a", flat=True).distinct() b_values = MyModel.objects.filter(b__icontains=substring).values_list("b", flat=True).distinct() unique_values = list(set([*a_values, *b_values])) Is it possible to rewrite it with one database request? -
Verify a Django model field inside a Django model
I have a Django model called Attendance that has the clock in and clock in times of an employee along with the status of that entry, to see whether it's authorized or not. I then, am making another model called Payroll. I want this to check inside the Attendance entries to see all the Authorized entries and then do some action on them. How do I check all the status fields for all the entries in Attendance? To better elaborate my question, this is how I've setup my Attendance model: class CWorkAttendance(models.Model): AUTO_ATT = "AU" MANUAL_ATT = "MA" WORK_ENTRY_TYPES = ( (AUTO_ATT, "Auto-Attendance"), (MANUAL_ATT, "Manual-Attendance"), ) AUTHORIZED = "AU" UNAUTHORIZED = "UA" WORK_ENTRY_STATUSES = ( (AUTHORIZED, "Athorized"), (UNAUTHORIZED, "Un-Authorized"), ) work_employee = models.ForeignKey('CEmployees', on_delete=models.CASCADE,) work_start_time = models.DateTimeField() work_end_time = models.DateTimeField(null=True) work_duration = models.IntegerField(null=True) work_entry_type = models.CharField(max_length=2,choices=WORK_ENTRY_TYPES) work_entry_status = models.CharField(max_length=2, choices=WORK_ENTRY_STATUSES, default=WORK_ENTRY_STATUSES[1][0]) If you look closely at the work_entry_status, it's a choice CharField that will contain the status of the entry (UNAUTHORIZED by default). I want to create a Payroll model that will check for all the rows in the CWorkAttendance model and check their work_entry_status fields to see if they are Authorized, which is what I want to learn how … -
How to integrate a python script to run when starting a Django project
I'm working on a Platform that uses Django and DjangorestFreamwork, so this platform is used to monitor the state of some sensors which sends data to the computer, so now i have the data in the computer i want to create a python file to read the data from the computer and store it in MySQL database that Django will read and show to the user I have two questions : - Can i make django read from the file save the data in the database and show it in the user interface ? if not How could i make a script to run in the background when i start the project ? -
Websocket gives error 500 once deployed but works on dev
I'm trying to deploy a django app using django-channels. When I run the server in a dev environment, it works perfectly. But when deployed, the websocket crash with an error 500. I use django 2.2.2, channels 2.2.0 and asgiref 3.1.4, on a linux debian stretch 9.9 VPS, with python 3.6 (compiled myself because not available on stretch) and nginx engine. I followed for deployment tutorials on https://djangodeployment.readthedocs.io/en/latest/01-getting-started.html and for the channels i tried to adapt the page https://channels.readthedocs.io/en/latest/deploying.html but mostly https://avilpage.com/2018/05/deploying-scaling-django-channels.html I'm quite noob to redis and all these things, at first I had redis working on port 6379, but it was with the main user of the system I used to do some tests in a dev environment, not the user for the django app. So I did another command to run docker on port 6479 for the django app user, which seemed to work. Then I had 404 error but it was because of nginx not configured properly for websocket. Now, I have the 500 error. here is the traceback of django when I try to open the websocket: [2019-08-19 12:54:46,857] ERROR: Exception inside application: Connection has been closed by server File "/opt/knightools/venv/lib/python3.6/site-packages/channels/sessions.py", line 183, in __call__ return … -
What's the model query of a raw SQL
I working on some kind of pricelist, with prices changing over time. I'm facing problems with retrieval of latest price for each product. My models are as follows: from django.db import models from django.core.validators import MinValueValidator, MaxValueValidator class Product(models.Model): id = models.PositiveIntegerField(primary_key=True, validators=[MinValueValidator(10000), MaxValueValidator(99999)]) name = models.CharField(max_length=100, null=False, blank=False) def __str__(self): return f'[{self.id}] {self.name}' class ProductPart(models.Model): product = models.ForeignKey('Product', on_delete=models.CASCADE, null=False) price = models.DecimalField(decimal_places=2, max_digits=7, null=False) date_created = models.DateTimeField(auto_now_add=True) date_changed = models.DateTimeField(auto_now=True) I have a raw SQL variant of this, but can't figure out how to turn it into a Django Query. The raw query is: select pp.id as product_id, pp.name as product_name, ppp.price as price from pricelist_Product as pp inner join pricelist_ProductPart as ppp on pp.id=ppp.product_id where (pp.id, ppp.id) in ( select pp.product_id, max(pp.id) from pricelist_ProductPart as pp group by pp.product_id ) Help me, please. -
Create model for existing database table in django
I have an old table in the database. And I want to create a model in Django application. After creating a model and I used migrate command then it created a new table with its own name. -
Create a django api that wil read and store the contents of a file
I need to create a django api that wil read the contents of a file and store it in the database. -
Check value in database and remove value from dropdown
I have loop, inside that there is a dropdown and I want to check in database and remove value from dropdown accordingly. {% for item in items %} <select name="somename" class="someclass"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> {% endfor %} So initially it looks like this. So when value "1" is selected then it should not display throughout the loop and it should not display if it is already selected and saved in database. -
form validation returns False
I'm dealing with form validation. For some mysterious reason my form.is_valid() returns false. I looked over other related topics here on SO but none of them helped. I'm getting no errors from form.errors. view: def index(request): form = TestCaseForm() if request.method == 'POST': if form.is_valid(): TestCase.objects.create( name=request.POST['name'], documentation=request.POST['documentation'], steps=request.POST['steps'], tags=request.POST['tags'], setup=request.POST['setup'], teardown=request.POST['teardown'], template=request.POST['template'], timeout=request.POST.get(int('timeout')), ) return redirect('/list') else: for i in form.errors: print(i) return render(request, 'index.html', {'form': form}) forms.py: class TestCaseForm(forms.Form): name = forms.CharField(widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter Name'}), label='') documentation = forms.CharField(widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter Documentation'}), label='') steps = forms.CharField(widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter Steps'}), label='') tags = forms.CharField(widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter Tags'}), label='') setup = forms.CharField(widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter Setup'}), label='') teardown = forms.CharField(widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter Teardown'}), label='') template = forms.CharField(widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter Template'}), label='') timeout = forms.CharField(widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Enter Timeout (optional)'}), required=False, label='') models.py: class TestCase(models.Model): """ The test case """ name = models.CharField(max_length=200) documentation = models.CharField(max_length=2048, blank=True) steps = models.CharField(max_length=2048, blank=True) tags = models.CharField(max_length=200, blank=True) setup = models.CharField(max_length=2048, blank=True) teardown = models.CharField(max_length=2048, blank=True) template = models.CharField(max_length=200, blank=True) timeout = models.IntegerField(default=10) def __str__(self): return self.name html: <form method="POST" action="/"> {% csrf_token %} {{ form.as_p }} <input type="submit" class="btn … -
Django create/save and atomic requests
I am running Django (1.11.20 with ATOMIC_REQUESTS: True) and Postgres, and have a Django view that basically does: job = Job(name='hello') job.save() files = JobFile.objects.create(job_id=job.id, file='myfile.txt') Most of the time, this works fine. But every so often, another process (that is executed by cron) checks the JobFile table and finds it not containing a record for a given existing Job, when doing: jobs = Job.objects.filter(timestamp=my_timestamp) JobFile.objects.filter(job__in=jobs) It reports this in a log and errors and when I check somewhat later the DB contains a JobFile just fine. I am scratching my head as to why it doesnt find JobFile records, and now I investigated and found that in the lastest occurrences of this problem, the cron process initiated about 0.1s before record creation and finished closely after, which made me suspicious that there is some sort of timing problem. But my (limited) understanding would be that when this is all in a single view, ATOMIC_REQUESTS would ensure that both objects exist. My other suspect is the create method but that seems to return nicely after calling save according to its source. What am I missing? -
login/check the username and password from database in Django website using post request
i have create a blog website where i want to do login of user whose username and password are saved in a database table name "myuser" i ll already fetch the username and password from post request in my function , NOW HOW I CAN COMPARE THIS VALUE FROM MY ALREADY CREATED TABLE VALUES TO CHECK USERNAME AND PASSWORD IS CORRECT OR NOT -
specify multiple variations for field in database using factory boy
I am new to using the tool django factory boy. I would like to know how can we specify multiple test data patterns for a field in database model using the factory class of django factory boy. -
How to prevent 404 error when retrieving an image from my static folder
My Django project includes a company logo which is stored inside the static folder and displayed in the navbar on each page. The logo displays on some pages, but for some reason it returns a 404 error on others. I can't spot any correlation between the pages that return a 404. Does anybody have any idea why I've encountered this problem? # base.html <a class="navbar-brand" href="{% url 'index' %}"> <img src="../static/img/acceler8_logo.png" id="acc-logo"> </a> # settings.py STATIC_URL = '/static/' STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), ) -
How to send email from just the user who are logged in to an official account?
I am trying to send email from the user who is logged in to an account which I have given bilalkhangood4@gmail.com. But I have typed it manually. I want whenever I log in, it should send an email only from that user account instead of writing it manually. How to do it? I used User also but it is giving me TypeError at /ViewSendEmail/ object of type 'ModelBase' has no len() settings.py EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.gmail.com' EMAIL_USE_TLS = True EMAIL_HOST_USER = 'bilalkhangood4@gmail.com' EMAIL_HOST_PASSWORD = '' EMAIL_PORT = 587 ACCOUNT_EMAIL_VERIFICATION = 'none' EMAIL_USE_SSL = False views.py from django.contrib.auth.models import User def ViewSendEmail(request): send_mail('Hello from Bilal Khan', 'Hello there, This is an automated message.', User, #FROM ['bilalkhangood4@gmail.com'], #TO fail_silently=False) return render(request, 'nextone/email_send.html') -
Procsssing csv files from upload in formtools (django)
Using formtools for django, I am trying to process the files being uploaded by the user in each step. So doing something likes this: def do_something_with_the_form_data(form_list): """ Do something, such as sending mail """ form_data = [form.cleaned_data for form in form_list] file = form_data[1]['bankA'] print(form_data[1]['file1']) f = open(file, 'rb') reader = csv.reader(f) ... is returning an error What is best practice for this? How can I properly read the files being uploaded by the user? P.S. Let me know if you want to see the forms or entire view -
Django: How can I include a variable within a nested for loop?
I need to include a Django template language variable within a nested 'for' loop to obtain page specific information in the dictionary. The structure of 'request.session.record_input' is: request.session.record_input = {'1':{'Data1':Data1, 'Data2':Data2} ,'2':{ etc... I've including it as just the variable name although I'm aware that this wouldn't work, this is purely to show where the variable would be. {% for page in request.session.record_input.keys %} <div class="container" style="text-align:center; vertical-align:middle;"> {% for key, value in request.session.record_input.page.items %} <div class="row"> <div class="col-2 centreObject"> <p1> {{ key|extract_comname }} </p1> </div> <div class="col-3 centreObject"> <p1>{{ value }}</p1> </div> </div> {% endfor %} </div> <hr> {% endfor %} I would normally access this in Python via: for page in request.session.record_input.keys(): for key, value in request.session.record_input[page].items() But you can use '[]' within the Django template language Any help will be greatly appreciated! -
How can I set query params in a url of django rest framework?
I have an API that i need to remove something from cache of Redis So I build a view and sync it to a url but I keep getting HTTP_404 (not found). class ExpireRoom(APIView): permission_classes = (IsAuthenticated, ) def delete(self, request, format=None): room_manager = RoomManager() room_identifier = self.kwargs['room_identifier'] is_success = room_manager.delete(room_identifier) if is_success: return Response(status=status.HTTP_200_OK) else: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR) Here's my url in urls.py: urlpatterns = [ url(r'^expire-room/(?P<room_identifier>\w+)/$', ExpireRoom.as_view(), name='core_ExpireRoom_deletingRoom') ] -
'Manager' object has no attribute 'filter_by_instance'
class Post(models.Model, HitCountMixin): user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE) title = models.CharField(max_length=120) slug = models.SlugField(unique=True) image = models.ImageField(upload_to=upload_location, null=True, blank=True, width_field="width_field", height_field="height_field") content = RichTextField() draft = models.BooleanField(default=False) publish = models.DateField(auto_now=False, auto_now_add=False) read_time = models.IntegerField(default=0) # models.TimeField(null=True, blank=True) #assume minutes updated = models.DateTimeField(auto_now=True, auto_now_add=False) timestamp = models.DateTimeField(auto_now=False, auto_now_add=True) tags = TaggableManager(blank=True, related_name="posts_tags") hit_count_generic = GenericRelation( HitCount, object_id_field='object_pk', related_query_name='hit_count_generic_relation') objects = PostManager() def __unicode__(self): return self.title def __str__(self): return self.title def get_absolute_url(self): return reverse("posts:detail", kwargs={"slug": self.slug}) class Meta: ordering = ["-timestamp", "-updated"] def get_markdown(self): content = self.content markdown_text = markdown(content) return mark_safe(markdown_text) @property def comments(self): instance = self qs = Comment.objects.filter_by_instance(instance) return qs @property def get_content_type(self): instance = self content_type = ContentType.objects.get_for_model(instance.__class__) return content_type -
django: allocate user.is_staff= True from permission group
I have tried to allocate is_staff to my new user, but it doesn't work. I have also tried if request.POST.get('group_name') == 'Staff': user.is_staff = True, without any success. views # Register a new user @login_required @group_required('Staff') def registerView(request): if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): user = form.save() if form.cleaned_data['group_name'] == 'Staff': user.is_staff = True group = Group.objects.get(name=request.POST.get('group_name')) user.groups.add(group) messages.success(request, "User has been added!") return redirect('accounts:users') else: form = RegisterForm() return render(request, 'accounts/register.html', {'form': form}) forms: class RegisterForm(UserCreationForm): first_name = forms.CharField(max_length=30, required=False) last_name = forms.CharField(max_length=30, required=False) Group = [('Viewers', 'Viewers'), ('Editors', 'Editors'), ('Creators', 'Creators'), ('Staff', 'Staff'), ] group_name = forms.ChoiceField(choices=Group) is_active = forms.BooleanField(initial=True, required=False) class Meta: model = User fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2', 'group_name', 'is_active', )