Running into a "Cannot assign... must be a ...instance" error
New to Django. I'm trying to set up a simple comment form where I can add a title for the comment, content of the comment, and attach the username of the user that is currently signed in and posting it. I run into the following error when running it: Cannot assign "'john'": "Entry.author" must be a "User" instance. I think I understand what's happening here, however I'm having a hard time figuring out how to implement it properly. *Here's my model code:* class Entry(models.Model): title = models.CharField(max_length=60) content = models.TextField(max_length=500) author = models.ForeignKey(User, on_delete=models.CASCADE, null=True) def __str__(self): return f"{self.id}" *Here's my view code:* @login_required def add(request): if request.method == "POST": entry = Entry( title = request.POST["title"], content = request.POST["content"], author = request.POST["username"] ) entry.save() return render(request, "articles/index.html", { "entries": Entry.objects.all() }) else: return render(request, "articles/add.html", { "username": request.user }) *And here's my form code in my template:* {% block body %} New Entry {% csrf_token %} Title: Content: Content goes here {% endblock %} -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e0bb05c3-ecee-4f75-97ca-a659cb7e6181%40googlegroups.com.
New to Django. Trying to wrap my head around markdown to html conversion using regex
Hello all, I'm working on a class assignment, so I'm not seeking a specific answer/code, but I'm just trying to figure out a general process. The task is this... I have some markdown files located in a directory, however when running the website they need to be displayed as html. Because this is a learning assignment, no markdown conversion libraries are allowed. So I'm tasked with using regular expressions to convert. I understand regular expressions and have already formulated a few to change things like **text** to text and so on, but being so new to Django and still figuring out how it works, I'm just completely flummoxed on the execution/implementation. So any general advice would be appreciated. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4d3fe147-d502-40c2-a866-d72bb30f8352%40googlegroups.com.
Django's str(queryset.query) returns invalid SQL if I compare datetimes
I have a Django queryset that I prepare with queryset.filter(date__gte=datetime(2011,1,1)) If I then call `str(queryset.query)` I see this in the string: ... WHERE "App_table"."date" >= 2011-1-1 However, this is invalid SQL code as if I run this in Postgresql I get this error: ... WHERE "App_table"."date" >= 2011-1-1 ERROR: operator does not exist: date >= integer HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. Why is this happening and how can I ask Django to output proper SQL code that I can work on? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3f7a7f2d-2e3d-4167-8b77-2b1c7da5446d%40googlegroups.com.
Re: Aggregating the results of a .union query without using .raw, is it possible?
My bad, the correct SQL query is this: SELECT "date", sum("car_crashes") FROM ( // String from Python str(aggregated.query) ) as "aggregated" GROUP BY "date" Il giorno lunedì 19 agosto 2019 23:10:47 UTC+2, Jo ha scritto: > > I have a table that looks like this > > datecar_crashes city > 01.01 1 Washington > 01.02 4 Washington > 01.03 0 Washington > 01.04 2 Washington > 01.05 0 Washington > 01.06 3 Washington > 01.07 4 Washington > 01.08 1 Washington > 01.01 0 Detroit > 01.02 2 Detroit > 01.03 4 Detroit > 01.04 2 Detroit > 01.05 0 Detroit > 01.06 3 Detroit > 01.07 1 Detroit > > > I want to know how many car crashes for each day happened in the entire > nation, and I can do that with this: > > > Model.values("date") \ > .annotate(car_crashes=Sum('car_crashes')) \ > .values("date", "car_crashes") > > > > Now, let's suppose I have an array like this: > > weights = [ > { > "city": "Washington", > "weight": 1, > }, > { > "city": "Detroit", > "weight": 2, > } > ] > > > > This means that Detroit's car crashes should be multiplied by 2 before > being aggregated with Washington's. > > It can be done like this: > > from django.db.models import IntegerField > > > when_list = [When(city=w['city'], then=w['weight']) for w in weights] > case_params = {'default': 1, 'output_field': IntegerField()} > > > Model.objects.values('date') \ > .annotate( > weighted_car_crashes=Sum( > F('car_crashes') * Case(*when_list, **case_params) > )) > > However, this generates very slow SQL code, especially as more properties > and a larger array are introduced. > > Another solution which is way faster but still sub-optimal is using pandas > to : > > aggregated = false > for weight in weights: > > ag = Model.values("date") \ > .annotate(car_crashes=Sum('car_crashes')) \ > .values("date", "car_crashes") > > > if aggregated is False: > aggregated = ag > else: > aggregated = aggregated.union(ag) > > > aggregated = pd.DataFrame(aggregated) > if len(weights) > 1: > aggregated = aggregated.groupby("date", as_index=False).sum(level=[1]) > > > This is faster, but still not as fast as what happens if, before calling > pandas, I take the aggregated.query string and > wrap it with a few lines of SQL. > > > SELECT "date", sum("car_crashes") FROM ( > > > // String from Python > str(aggregated.query) > > > ) > > > This works perfectly when pasted into my database SQL. I could do this in > Python/Django using .raw() but the documentation says to ask here before > using .raw() as mostly anything could be acomplished with the ORM. > > Yet, I don't see how. Once I call .union on 2 querysets, I cannot > aggregate further. > > aggregated.union(ag).annotate(cc=Sum('car_crashes')) > > gives > > Cannot compute Sum('car_crashes'): 'car_crashes' is an aggregate > > > > > Is this possible to do with the Django ORM or should I use .raw()? > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7096fdff-ec55-4d1b-8110-9fd6bb70f9e1%40googlegroups.com.
Aggregating the results of a .union query without using .raw, is it possible?
I have a table that looks like this datecar_crashes city 01.01 1 Washington 01.02 4 Washington 01.03 0 Washington 01.04 2 Washington 01.05 0 Washington 01.06 3 Washington 01.07 4 Washington 01.08 1 Washington 01.01 0 Detroit 01.02 2 Detroit 01.03 4 Detroit 01.04 2 Detroit 01.05 0 Detroit 01.06 3 Detroit 01.07 1 Detroit I want to know how many car crashes for each day happened in the entire nation, and I can do that with this: Model.values("date") \ .annotate(car_crashes=Sum('car_crashes')) \ .values("date", "car_crashes") Now, let's suppose I have an array like this: weights = [ { "city": "Washington", "weight": 1, }, { "city": "Detroit", "weight": 2, } ] This means that Detroit's car crashes should be multiplied by 2 before being aggregated with Washington's. It can be done like this: from django.db.models import IntegerField when_list = [When(city=w['city'], then=w['weight']) for w in weights] case_params = {'default': 1, 'output_field': IntegerField()} Model.objects.values('date') \ .annotate( weighted_car_crashes=Sum( F('car_crashes') * Case(*when_list, **case_params) )) However, this generates very slow SQL code, especially as more properties and a larger array are introduced. Another solution which is way faster but still sub-optimal is using pandas to : aggregated = false for weight in weights: ag = Model.values("date") \ .annotate(car_crashes=Sum('car_crashes')) \ .values("date", "car_crashes") if aggregated is False: aggregated = ag else: aggregated = aggregated.union(ag) aggregated = pd.DataFrame(aggregated) if len(weights) > 1: aggregated = aggregated.groupby("date", as_index=False).sum(level=[1]) This is faster, but still not as fast as what happens if, before calling pandas, I take the aggregated.query string and wrap it with a few lines of SQL. SELECT "date", sum("car_crashes") FROM ( // String from Python str(aggregated.query) ) This works perfectly when pasted into my database SQL. I could do this in Python/Django using .raw() but the documentation says to ask here before using .raw() as mostly anything could be acomplished with the ORM. Yet, I don't see how. Once I call .union on 2 querysets, I cannot aggregate further. aggregated.union(ag).annotate(cc=Sum('car_crashes')) gives Cannot compute Sum('car_crashes'): 'car_crashes' is an aggregate Is this possible to do with the Django ORM or should I use .raw()? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3e3e80a1-29e4-47bd-8688-f826063d2b02%40googlegroups.com.
Re: Model share question
That sounds right. Thank you. On Thursday, March 31, 2016 at 2:19:45 AM UTC-4, Mike Dewhirst wrote: > > On 31/03/2016 8:38 AM, moon jo wrote: > > I'm new to django and have a question regarding model design. > > For practice, I'm working on a web app - a simplified version of imdb > > that handles movie and music. > > I've created 2 apps; Movies and Music. With main models in both having > > the usual fields (title, release date, rating, artist/actor). > > My problem is, I don't know where to put the Person model. I want it to > > be shared by both apps. > > Should there be an app for Person and put the model in there? > > Short answer is yes. Long answer is yes. > > Once you put the app 'person' into INSTALLED_APPS in settings in each of > the other two apps you can exploit 'person.Person' in foreign keys and > Django will find it. If you prefer using the person model directly you > do "from person.models import Person" then just use Person in foreign > keys. > > > Thanks. > > > > -- > > You received this message because you are subscribed to the Google > > Groups "Django users" group. > > To unsubscribe from this group and stop receiving emails from it, send > > an email to django-users...@googlegroups.com > > <mailto:django-users+unsubscr...@googlegroups.com >. > > To post to this group, send email to django...@googlegroups.com > > > <mailto:django...@googlegroups.com >. > > Visit this group at https://groups.google.com/group/django-users. > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/django-users/d28d97f6-37c6-4ef5-89d3-96d538e86b2c%40googlegroups.com > > > < > https://groups.google.com/d/msgid/django-users/d28d97f6-37c6-4ef5-89d3-96d538e86b2c%40googlegroups.com?utm_medium=email_source=footer>. > > > > For more options, visit https://groups.google.com/d/optout. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2104a588-cb08-42cf-a26c-6259bee1c7c8%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Model share question
I'm new to django and have a question regarding model design. For practice, I'm working on a web app - a simplified version of imdb that handles movie and music. I've created 2 apps; Movies and Music. With main models in both having the usual fields (title, release date, rating, artist/actor). My problem is, I don't know where to put the Person model. I want it to be shared by both apps. Should there be an app for Person and put the model in there? Thanks. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d28d97f6-37c6-4ef5-89d3-96d538e86b2c%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
ImportError: no module named polls
Using django 1.6.10 I finished the polls tutorial and tried to create my own project. I tried to runserver and it gives me the import error. But it's a blank project, haven't added anything in the settings. When I do import settings form django.conf and print settings.INSTALLED_APPS, it prints this: ('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'polls') Where is that 'polls' coming from? Every project I create, I get the same error, they're looking for the 'polls' app. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/df91e3ff-aa9e-424a-9db2-f8d7091003a3%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Is it possible to connect django to apache derby as a database?
I am a newbie about Django and Python. But I attended the basic lecture of python. So I will implement my web application. I want to use apache derby. Is is possible? I am proud of being one of member in this group. Thanks. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5c3529de-7010-40ed-9ad0-71294c12f14e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Cannot start tutorial that begins with '"jango-admin.py startproject mysite"
Hello. I believe I have successfully installed Django based on having no problems with testing "import django" at the command prompt and in the Python interpreter. However, I cannot get the tutorial started. As explained by the tutorial ("From the command line, cd into a directory where you’d like to store your code, then run the following command: django-admin.py startproject mysite") I cd over to: C:\Users\John. When I type "django-admin . . ." nothing happens. "mysite" is not created. I have looked at group messages. One thing I notice is that I appear to be missing "Lib\site-packages\django\bin", but I don't know how to find/download this. Also, at the command prompt when I cd to Python27\Scripts and enter "django-admin.py startproject mysite" I get "Import Error: No module named django.core". My set-up: 1. Windows 7 32-bit 2. C:\Python27\Scripts\django-admin.py 3. End portion of my System Path as shown in Environmental Variables: . . ;C:\Python27\Scripts; C:\Python27\Lib\site-packages\django\bin; C:\Python27\Scripts\django-admin.py -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7df34208-487d-41b7-87a4-646d9b883ad6%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: What is the best way to implement time-based / cronjob actions in a Django app?
On 13 ?.?. 2010, at 18:50, Jonathan Barratt wrote: > On 13 ?.?. 2010, at 18:38, Mattias Linnap wrote: > >> Hi Django users, >> >> I'm building an application with Django, and I need some database >> changes to occur at certain times in the future - independently of any >> web request. Things like cleaning up expired sessions, deleting >> expired user actions, etc. >> >> What is the best way to implement those? Having now checked it out for use in my own project, the '"Celery" suggestion that's been made to you definitely sounds like the most 'correct' way to implement this. Thanks Tom and Brian! It is, however, as Tom admitted, rather complex. I had another idea that I figured was worth throwing out there for your consideration as it's a lot easier and faster to implement, though correspondingly not nearly as flexible or powerful: just use the *nix command "at." When someone reserves an item just execute something along the lines of: (os.popen.) popen('echo "COMMAND "' + PARAMETERS + ' | at now + 2 hours' This is nowhere near as robust or efficient as I'm sure Celery is, but it's so much simpler and quicker that I thought it worth mentioning in case you don't have the time to get Celery going right away and need something to use for demo\testing purposes in the meantime... Just a thought, Jonathan -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
How to manually set FileField path
Hi, I need to set the content of a FileField manually, ie not using the admin screens. The instance of the model that contains the FileField already exists, but has the FileField is currently none. I know the local path for the file, but just can't work out how to update the FileField so I can save the object. Thanks for any help Jo --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Django documentation site is SLOW
Ok, after a bit of tinkering I think I might have found the culprit. Seems fine in Opera, and much better in Firefox when I disable Adblock Plus (the only add-on I've got installed!) Still a little sluggish in Firefox, but they are big pages on quite an old machine. Goes from unusable to almost normal with ABP disabled. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Django documentation site is SLOW
Surely can't only be me that finds the main Django site painfully slow? There is some javascript in there or something that just kills my browser. I'm using Firefox on Linux, on 1.5gig P4, OK not state of the art but it's fine for pretty much any other website, but when I try to look something up on the Django website, firefox jumps to 80% CPU and takes 10s of seconds to render each page, and even when the page is rendered the browser is so sluggish as to be almost unusable. Django might speed up development, but what it gives it takes away in time sitting waiting for the damn docs to load! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: template inheritance
On Jan 12, 10:22 am, bruno desthuilliers <bruno.desthuilli...@gmail.com> wrote: > On 11 jan, 18:46, Jo <spaceout...@gmail.com> wrote: > > > While building a website using template inheritance one usually does > > the following: > > > fetch from database > > fetch from some more data from database > > ... << more required computations > > then at the end render the template with the fetched data > > > Without template inheritance one usually does the following: > > > fetch from database > > render this part of the site with this fetched data > > fetch some more data from the database > > render this other part of the site with the fetched data > > This has nothing (or very few) to do with inheritance. You'd have the > same behaviour (ie : first getting all data, then building html, then > return the response) without using inheritance in your template. Yes but template inheritance usually leads to this design. > > Is there a way to get the best of both > > worlds? Maybe using generators? Thank you. > > You can of course pass an iterable to your reponse object (read the > doc for the HttpResponse object), but then you'll have to organize > your code very differently, and not necessarily in the most readable > and maintainable way. > > As far as I'm concerned, I'd first try to profile and possibly > optimize the "very long computation" part. Yes but you'd have to agree that there is a loss. It would be nice to render data as it comes. Instead of waiting for all the computation to finish. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
template inheritance
While building a website using template inheritance one usually does the following: fetch from database fetch from some more data from database ... << more required computations then at the end render the template with the fetched data Without template inheritance one usually does the following: fetch from database render this part of the site with this fetched data fetch some more data from the database render this other part of the site with the fetched data ... etc The first approach is much more elegant and leads to easier to maintain code. However the user will have to wait till the end of the fetching (long computations) before any rendering can take place. In the second approach however the site loads as we are fetching the data which is more convenient to the user. Am I correct to assume this? Is there a way to get the best of both worlds? Maybe using generators? Thank you. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
hi guys.............. did u know the money making secrets.........
hi guys.. did u know the money making secrets. we will give the oppertunity if u are interesting in online jobs. if u joined with us then earn $400 per day.. just work in 1 hour per daylog in our site www.jobsforyouguys.blogspot.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
did u know the money making secrets.............
did u know the money making secrets. google groups brings u these oppertunity .. u submit a project to google groups then u get $4 per project work.. do u want to join dis work visit our site www.jobsforyouguys.blogspot.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Django vs TurboGears
If there is anybody interested to give professional support to a softwarehouse in Bologna-Italy to start a project using Django, please contact me at: [EMAIL PROTECTED] --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---
Django vs TurboGears
Hi all, I'm evaluating the possibility to use Django for a project, but I have read so many comments about Django vs TurboGears and right now I am very confused. I installed both of them in my system and sincerelly I found that Django is very easy to install and getting started while TG is not so easy. Nevertheless I hesitate to trust on the Django semplicity against the TG complexity and I'm trying to examinate some characteristics in both of them that interest to me. Django gives you an admin environment for free while TG doesn't have such thing and I have no idea how to create it. TG applys the pattern MVC while Django does it in a strange way. Django doesn't use AJAX while TG uses Mochikit and JASON. Django is very compact while TG is assembled with many moduls to put together. Someone of you could give me a good reason to use Django instead of TG? Thank you, jo --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~--~~~~--~~--~--~---