Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django Sever deleting a files content, even if file opened in read mode
I am in good trouble, i am reading a file in django, opened in read mode. After the logic executes the program that opens the file, i close the file gracefully at the end of the program. But as i restart, that is either kill or start django server, the contents of that file are washed away, even though its only opened in read mode. HELP ME PLEASE!! :') i have ran the program separately and it doesnot deletes the file, but restarting the server deletes it. Feel free to ask if you need more detail :D Thanks in advance :) function that calls the program: ` class Log_details(APIView): def get(self, request,format='json'): file=open('/home/tcs/Desktop/djangologs2.txt','r') data=file.read() file.close() result2 = featurelog.func3(data) return Response(result2) ` Here is the program code for func3: ` import re def func3(file): res=file match1= re.re.findall(r'.*:(\w+):(TestCase\d+).*(Passed|Failed|Errored).*', res) print(match1) dict1={} j=1 k=2 l=0 # print(len(match1)) dict1={} list5=[] print("match1") print(match1) if(len(match1)==0): return {'no logs':[{'name':"no logs1",'result':"no logs2"}]} else: for i in range(len(match1)): #if key is not present empty the list if match1[i][l] not in dict1: list5=[] # print(match1[i][l],(match1[i][j] + ':' + match1[j][k])) list5.append({'name':match1[i][j],'result' : match1[i][k]}) dict1[match1[i][l]] =list5 print(dict1) return (dict1) ` -
How to store chat messages into database
I created chat application based on Django. It works: I can write messages into input field and receive response. But I can't save them into Postgres database. Technologies I used: Django==1.9.7 django-websocket-redis==0.4.5 Redis Postgres database So, tasks I need to overcome: Save messages into DB Retrieve them from DB Represent them with my CSS styles Should I use querysets in views or write raw SQL statements, would it hit database performance (this chat app goes into production), or should I save them into Redis database and then retrieve messages from Redis? Any recommendations appreciated. -
How to send channel_session data from outside the consumer class
I am facing problem in sending data from channel_session outside the consumer class. My consumer is like :- class myconsumer(AsyncWebsocketConsumer): #all init connect function def subscribe(self): self.channel_Session = payload['data'] #some other functions Now the problem is, I want to send this self.channel_session through an async function which is called as a thread. async def sendData(): while True: #send data await asyio.sleep(5) class thread(multiprocess.Process): try: loop = asyncio.bew_event_loop() except Exception as e: loop = asyncio.get_event_loop() loop.run_until_complete(sendData()) This Thread is started on starting the server. Question I cannot find a way to send that channel_session Any help would be appreciated. -
Django model for an associative table in legacy database
Since Django doesn't support composite primary keys, what is the best practice for implementing the model for an associative (or junction) table? I have a table that maps user_id to user_type_id with the pair forming a composite primary key. The user and user_type field were originally both ForeignKey classes, but the interpreter gave a hint to use OneToOneField instead. I get no warnings with the code below. Is this the proper way to implement the table? class UserUsertype(models.Model): user = models.OneToOneField( 'User', on_delete=models.DO_NOTHING, db_column='user_id', primary_key=True) user_type = models.ForeignKey( 'Usertype', on_delete=models.DO_NOTHING, db_column='user_type_id ') class Meta: db_table = 'user_userType' unique_together = (('user', 'user_type'),) -
Django DateTimeField showing up as string
Hi one of my Django models has a the following field created_at = models.DateTimeField(blank=True,null=True) However when I check the data type it shows as String print(type(self.created_at)) <class 'str'> Funny thing is the same code running on my production server shows it as <class 'datetime.datetime'> I am using python3 and Django 1.10.6. Any idea what is causing this ? -
How django ensure one persistent connection doesn't shared by multithread at the same time?
From django db doc , when use persistent connection(set CONN_MAX_AGE > 0 or None), it won't be closed after each request util it exceeds the maximum age or when it isn’t usable any longer. That's being said, the following reuqests will use the same persistent connetion that has being created before, which may cause one persistent connection shared by multithread at the same time. But this situation shouldn't happen! I'm just confused and I believe django developers won't make such shallow mistake. Or do I miss something? Thanks in advance! -
analogue of bulk add one object to different rows with manytomanyfield
I was looking for possibility to add single object to different manytomanyfields For example I have 1000 articles and for some reason I need to add one more author to all of them, Is for the only option nowadays? -
Django error with createsuperuser and custom user model
I start a new project with django 2 and I create a custom user model, but when I try to create a superuser with manage.py createsuperuser I got the following error: Traceback Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "/home/goldman/.virtualenvs/organize/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line utility.execute() File "/home/goldman/.virtualenvs/organize/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/goldman/.virtualenvs/organize/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv self.execute(*args, **cmd_options) File "/home/goldman/.virtualenvs/organize/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 59, in execute return super().execute(*args, **options) File "/home/goldman/.virtualenvs/organize/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute output = self.handle(*args, **options) File "/home/goldman/.virtualenvs/organize/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 179, in handle self.UserModel._default_manager.db_manager(database).create_superuser(**user_data) TypeError: create_superuser() got multiple values for argument 'email' models.py from django.contrib.auth.models import AbstractBaseUser from django.contrib.auth.models import PermissionsMixin from django.db import models from django.utils.translation import gettext_lazy as _ from shortuuidfield import ShortUUIDField from .managers import UserManager class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField(_('email address'), max_length=255, unique=True) uid = ShortUUIDField(_('external ID'), db_index=True) date_joined = models.DateTimeField(_('date joined'), auto_now_add=True) is_active = models.BooleanField(_('active'), default=True) organization = models.ForeignKey( 'users.Organization', on_delete=models.SET_NULL, null=True, verbose_name=_('organization'), related_name='users') USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] objects = UserManager() class Meta: verbose_name = _('user') verbose_name_plural = _('users') ordering = ('organization', 'is_active') def __str__(self): return self.email def get_full_name(self): return self.email def get_short_name(self): return self.email managers.py from django.contrib.auth.models import BaseUserManager from django.db import models … -
How to display all the information of a profile (user) in a single template in Django?
How do I show many forms in a view, and this one at the same time send it to an html template? What I need is to extract information from many related models but only from a specific user. I do not put the models because they are enough. -
customize save function of rest-auth registration
I have a user model something like this class Role(models.Model): ROLE_CHOICES = ( ('agent', 'Agent'), ('agency', 'Agency'), ('manufacturer', 'Manufacturer'), ) role = models.CharField(max_length=15, choices=ROLE_CHOICES) def __str__(self): return 'role' class User(AbstractUser): role = models.ForeignKey( Role, on_delete=models.CASCADE, blank=True, null=True) def __str__(self): return str(self.id) The user can be of 3 types. But at the time of registration, the user should be created with the role of agent.The endpoint for the user registration is /rest-auth/registration. This will create only a normal user with username and password. But i need to create the agent role as well. I believe, it should be done in save method of rest-auth registration serializer but I don't know how to customize the registration serializer. Can anyone shed me the light, please? -
Nesting a post request inside a post request with Django Rest Framework
I'm new to Django Rest and JWT token based authentication. I'm using a package called djangorestframework_simplejwt I have my frontend UI created and successfully authenticating users. I am having one issue, when I create a new user, I am sending the user back in the response data and then in a promise hitting the authentication endpoint to retrieve my tokens. This doesn't feel right and feels like something that the backend can take care of entirely. On the front end - return APIUtil.signup(user).then((user) => { return APIUtil.authenticate(user.data) }).then((user) => dispatch(authenticateUser(user))) export const authenticate = (user) => { return axios({ method: 'post', url: 'http://localhost:8000/api/token/', data: { username: user.username, password: user.password } }); } export const signup = (user) => { return axios({ method: 'post', url: 'http://localhost:8000/signup/', data: { username: user.username, password: user.password, email: user.email } }); } In my backend, I have a user viewset and serializer and with the django rest framework simple JWT package, I have the end point api/token/ which takes a user and returns their auth tokens. I want to create the user and after creation, hit that endpoint on the backend, but I am not entirely sure the best way of doing this. I tried inheriting … -
Redirect to its parent address if a given url not existing
I searched the django tag and see the url https://stackoverflow.com/questions/tagged/django, When I popped "django" from the trailing to be https://stackoverflow.com/questions/tagged, It redirects to an existing page of https://stackoverflow.com/questions rather than raise an 404. It's amazing, I'd like to add this function to my project, but find it hard to describe such an situation in google search. -
django template child queryset
I am trying to display fields from child model 'usertraining' entry linked by foreign key to a 'training' entry. but second queryset usertraining_set does not seem to contain anything. What am I missing? I have the following template loop: {% for training in training_list %} {% for elem in training.usertraining_set.all %} <div class="training"> <div class="training-info"> <div class="training-main-title"> <div class="training-state">{{ elem.state }}</div> <a href="{% url 'my_app:training_detail' pk=training.pk %}">{{ training.title }}</a> </div> <div class="training-date"> <p>Last trained: {{ elem.last_training_date|date:"D, d M Y, H:i:s" }}</p> </div> <div class="training-score"> Score: <span class="badge btn-circle">{{elem.score}}</span> </div> </div> <img src="{{ training.title_pic.url }}"> </div> {% endfor %} {% endfor %} model class Training(models.Model): title = models.CharField(max_length = 2000, blank = False) title_pic = models.ImageField(upload_to = 'training/title_pics', blank = False) def __str__(self): return self.title def get_absolute_url(self): return reverse("training_detail",kwargs={'pk':self.pk}) @property def title_pic_url(self): if self.title_pic and hasattr(self.title_pic, 'url'): return self.title_pic.url class UserTraining(models.Model): user = models.ForeignKey(User,related_name='usertraining',on_delete=models.CASCADE) training = models.ForeignKey(Training,related_name='usedtraining',on_delete=models.CASCADE) state = ( ('train', 'train'), ('training', 'training...'), ('retrain', 'retrain'), ('trained','trained') ) state = models.CharField(max_length = 200, choices = state, default = 'train') last_training_date = models.DateTimeField(auto_now=True) score = models.CharField(max_length = 4, default = '0%') def __str__(self): """ String representation of Training entry """ return "{}_{}".format(self.user,self.training) -
Why aren't my mocked methods being called?
A user can have many surveys. And I generate a report for the user by first accessing the user's most recent survey. I'm trying to mock out that function and just return a survey id = 1. But my mocked function is never being called. Here's the code: The User Model @python_2_unicode_compatible class User(AbstractUser): ... name = models.CharField(_('Name of User'), blank=False, max_length=255) ... ##### this is the function I want to mock ##### def get_survey(self): print("get real survey") return self.surveys.all().order_by('-updated_at')[0] def get_surveyid(self): return self.get_survey().surveyid The Survey Model class Survey(models.Model): '''The Django Survey Model''' surveyid = models.IntegerField(_('Survey ID'), primary_key=True, default=0) surveyname = models.CharField(_('Name of Survey'), blank=True, max_length=255, default='surveyname') issurveyactive = models.BooleanField(_('Is Survey Active'), default=True) user = models.ForeignKey(user_models.User, related_name='surveys', on_delete=models.DO_NOTHING,) . . . The Report View class UserReportView(LoginRequiredMixin, TemplateView): model = User template_name = 'users/reports/rptemplate/report.html' def get_context_data(self, **kwargs): theuser = self.request.user thesurvey = theuser.get_survey().surveyid reportdata = theuser.get_survey().createreport() ... return context The Test class BaseUserTestCase(TestCase): def setUp(self): self.user = self.make_user() self.factory = RequestFactory() @contextmanager def mock_user_get_survey(): mock_instance = mock.Mock(spec=Survey) mock_instance.surveyid = 1 print("mock_user_get_survey") #theuser.get_survey().createreport() class TestUserReportView(BaseUserTestCase): @patch('pulsemanager.users.models.User.get_survey', side_effect=mock_user_get_survey) def setUp(self, arg2, arg3): # call BaseUserTestCase.setUp() super(TestUserReportView, self).setUp() # Instantiate the view directly. Never do this outside a test! self.view = UserReportView() # Generate … -
Raw INNER JOIN REST FRAMEWORK
Como puedo utilizar raw de django en django rest framewok, pero utilizando INNER join, tengo el siguiente serializador class PcprovinciaSerializer(serializers.ModelSerializer): class Meta: model = Pcprovincia fields ="__all__" class LolViewSet(viewsets.ModelViewSet): queryset = Pcprovincia.objects.all() serializer_class = PcprovinciaSerializer module = "lol" def list(self, request, *args, **kwargs): try: queryset = self.queryset.raw("SELECT * FROM PCProvincia INNER JOIN PCPais ON idpais=idpais") serializer = self.get_serializer(queryset, many=True) return Response(serializer.data) except Exception as e: return Response({"error":str(e)}) it generates the following error error HOW CAN I SERIALIZE THE INNER JOIN? -
Return non null field in Many To Many intermediary class/model Django
I think this is a python question, but might have a django framework ¿query/or-the-like? I have an intermediary class/model: class VehicleFeatures(models.Model): personal_equipment = models.OneToOneField(PersonalEquipment, null=True, blank=True, ) commercial_equipment = models.OneToOneField(CommercialEquipment, null=True, blank=True, ) personal_vehicle = models.OneToOneField(PersonalVehicle, null=True, blank=True, ) commercial_truck = models.OneToOneField(CommercialTruck, null=True, blank=True, ) commercial_tractor = models.OneToOneField(CommercialTractor, null=True, blank=True, ) def __unicode__(self): # assume one and only one is allowed return '{}'.format(self.whichever_of_the_fields_exists) # Extra Credit :) # assume any number is allowed Notes: trying to follow option 2 and 3 in this article I am not worried about performance (yet) but if you are aware of a low cost option, then please teach us why one is better than another Visual of models 121-Referent: Vehicle (abstract model) with concrete models [listed above] M2M: VehicleFeatures (to connect to concrete models) 12M: Features (fields for many vehicle features) How do I return the Model.field that has a reference (ultimately to get that model's name, to pass back to the other side of this many-to-many)? -
How to change Django model with content in the database
I have a model with a CharField that has a choices set, but I need to change the field to a ForeignKey field. I have made the changes but, when I make the migration, I got an error saying django.db.utils.DataError: invalid input syntax for integer: "GRAND_TOURISM", that I realize means that the current content of the field doesn't match the new foreign reference constraint. Is there any way I can instruct Django to change current values to Null when migrating? -
Authentication to shopify store with partner app using Django
I have a shopify partner store and I would like to be able to make api calls. My app is using django, and this: https://github.com/Shopify/shopify_python_api from django.shortcuts import redirect import shopify def home(request): API_KEY = 'blabla' SHARED_SECRET = 'blabla' shop_url = "http://mystoretest112.myshopify.com/admin" shopify.ShopifyResource.set_site(shop_url) shopify.Session.setup(api_key=API_KEY, secret=SHARED_SECRET) session = shopify.Session("mystoretest112.myshopify.com") scope=["write_products"] permission_url = session.create_permission_url(scope,"http://localhost:8000/login") return redirect(permission_url) def login(request): code = request.GET['code'] signature = request.GET['hmac'] timestamp = request.GET['timestamp'] shop = request.GET['shop'] API_KEY = 'blabla' SHARED_SECRET = 'blabla' shop_url = "http://mystoretest112.myshopify.com/admin" shopify.ShopifyResource.set_site(shop_url) shopify.Session.setup(api_key=API_KEY, secret=SHARED_SECRET) session = shopify.Session("mystoretest112.myshopify.com") params = {'timestamp':timestamp,'code':code,'shop':shop,'signature':signature} token = session.request_token(params) print(token) So when I go to home with my app, I get redirected as expected. However, when I get to login, the call to request_token fails with this exception: "Invalid HMAC: Possibly malicious login" instead of giving me the permanent token I'm requesting. I tried calculating the hmac myself with the data given as shown here : https://help.shopify.com/en/api/getting-started/authentication/oauth#verification Every time I get the same hmac sent in the url. -
Using Celery, how do I post rabbitmq messages to another docker container?
It appears my celery workers launch and connect properly on the mymachine.domain.com:port where the rabbit mq resides in a separate docker container. However when I call the function apply_async from my web application it tries to connect on localhost:port even though it should be using the same django src/settings.py file which would also be mymachine.domain.com:port again. The only way I can get this to work is by setting the docker network_mode to 'host'. This is not an acceptable mode though for security reasons. Here is my celery.py file: from celery import Celery from celery.schedules import crontab os.environ.setdefault('DJANGO_SETTINGS_MODULE','src.settings') import django django.setup() from src.apps.main.tasks import cleanup, syncldap app = Celery('dysoncelery', backend='amqp', # Expected next line isn't even used because line with 'django.conf:settings' will override it with the config settings. broker='amqp://user@mymachine.domain.com:5672//', include=['src.apps.appname.tasks']) app.config_from_object('django.conf:settings', app.autodiscover_tasks() Here is my src.settings.py file settings: CELERY_BROKER_URL = 'amqp://user:password@mymachine.domain.com:5672//' CELERY_RESULT_BACKEND = 'django-db' CELERY_ACCEPT_CONTENT = ['json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' CELERY_DEFAULT_QUEUE = 'celery' I added a print statement to the following installed file file: site-packages/kombu/connection.py In the function: def default_channel(self): I added this print statement: print "Connection %s" % self.connect From my celery workers I see this in the logs: Connection <bound method Connection.connect of <Connection: amqp://<user>:**@mymachine.domain.com:5672// at … -
Geopostion field is required
I enter all values correctly but when I hit save it gives following error. I don't know why. My model is as: class Shops(models.Model): shopkeeper = models.ForeignKey(Shopkeeper, on_delete=models.CASCADE) name = models.CharField(max_length=250) address = GeopositionField() category = models.CharField(max_length=250) contactNumber = models.BigIntegerField() dateCreated = models.DateTimeField(auto_now_add=True) Please help me. -
On production sometimes receive IntegrityError: duplicate key value violates unique constraint
I have an API site that receives JSON data and save it to database. And sometimes on production server I receive error that I can't reproduce on my dev server: IntegrityError: duplicate key value violates unique constraint I use Django 2.0 and Django Rest framework I also use transactions: ATOMIC_REQUESTS = True My code looks like(the name field is unique=True): for key in data['some_data']: my_data_serializer = MyDataSerializer(data={ 'name': key }) if my_data_serializer.is_valid(): my_data_serializer.save() What can cause this. If I send on dev server data like this: { 'some_data': ['one', 'two', 'one'] } It works fine and ignore duplicates -
Why the crossdomain request returns an error "Provisional headers are shown"?
I have frontend (http://localhost:4200) on angular6 and backend (http://127.0.0.1:8000) on django1.8. On backend my view looks like that: def display_form(request): response = HttpResponse('set csrf cookie for form') rotate_token(request) response['Access-Control-Allow-Origin'] = 'http://localhost:4200/login' response['Access-Control-Allow-Credentials'] = 'true' response['Access-Control-Allow-Headers'] = 'X-CSRF-TOKEN' return response On frontend my request function looks like that: const xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.open('GET', Config.host + 'users/display_form', true); xhr.onload = function() { console.log( 'xhr req ok' + xhr.status + xhr.statusText + xhr.responseText); } xhr.onerror = function() { console.log( 'Ошибка ' + this + xhr.status + xhr.statusText + xhr.responseText); console.dir(this); } xhr.send(); But after send xhr request i get follow error message in chrome network tab. I need make crossdomain request successfully and set cookie to browser cookie storage. Please help me. PS: If i use this chrome extension then request is successfull -
Django renders the form without field errors and without help_text
I'm using django 2.0.7 I made user authentication form and login view when the form is submitted with wrong data or empty, it doesn't render field errors Here is a part of my template that has the logic of rendering field errors: {% if form.username.errors %} <div class="text-danger"> <small>{{form.username.errors.0}}</small> </div> {% endif %} {% if form.username.help_text %} <div class="col form-text"> {{form.username.help_text}} </div> -
Firebase Cloud Messaging - Error calling firebase.messaging()
I am getting an error when calling firebase.messaging() from the html page (not the service worker) that makes no sense. Messaging: Please ensure that 'messagingSenderId' is set correctly in the options passed into firebase.initializeApp(). (messaging/bad-sender-id). The app IS initialized with the correct messagingSenderId in a <script> in the <head>. The default app is shown in the console log: function deviceRegistration() { console.log('in deviceRegistration()'); console.log("firebase.apps:", firebase.apps); console.log("firebase.apps.length: ", firebase.apps.length); if (firebase.apps.length == 0) { // firebase.initializeApp({{ fcm_config.messagingSenderId }}); firebase.initializeApp({"messagingSenderId": "1234567890"}); } console.log("after initializeApp"); // prints to console var messaging = firebase.messaging(); // error happens here console.log("after firebase.messaging()"); // doesn't print to console The reason I only call firebase.initializeApp if there are no apps in firebase.apps is because when I call firebase.initializeApp without first performing this check I get the following error: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app). I thought I'm supposed to be able to call initializeApp repeatedly on every page and it would register the service worker if necessary... Can someone please help me figure out what's going on? Wwaaaaaa -
How to do this in Django?
I need to post a Memorandum to a user. What I can not do is extract the user that I want to leave the Memorandum (to) and also I can not give the user who wrote it (from). I use the Django User Model class Employee(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key = True) *The other fields ignored (for this example) class Memorandum(models.Model): from = models.ForeignKey(User) to = models.ForeignKey(Employee) date = models.DateField(default=datetime.date.today) subject = models.TextField() detail = models.TextField() POSTDATA: Another thing, how to pass the user (to) in a URL?