These are chat archives for django/django

25th
Oct 2018
Tanveer Ahmed
@tanveerobjects_twitter
Oct 25 2018 05:30
Hi Need Help I don't get output as related artworks | def get_context_data |
Please Need Help Here is my Code
import random
class art_detail(DetailView):
    model = Artwork
    template_name = 'gallery/art_detail.html'
    pk_url_kwarg = "art_id"
    def get(self, request, art_id):
        artwork = get_object_or_404(Artwork, id=art_id)
        try:
            next_artwork = artwork.get_next_by_published_date()
            while next_artwork.gcategory != artwork.gcategory:
                next_artwork = next_artwork.get_next_by_published_date()
        except Artwork.DoesNotExist:
            next_artwork = None
        try:
            previous = artwork.get_previous_by_published_date()
            while previous.gcategory != artwork.gcategory:
                previous = previous.get_previous_by_published_date()
        except Artwork.DoesNotExist:
            previous = None
        return render(request, 'gallery/art_detail.html', 
            {'artwork': artwork, 'next': next_artwork, 'previous': previous})
    def get_context_data(self, *args, **kwargs):
        context = super(art_detail, self).get_context_data(*args, **kwargs)
        instance = self.get_object()
        #order_by("-title")
        context["related"] = sorted(Artwork.objects.all()[:6], key= lambda x: random.random())
        return context
kpagcha
@kpagcha
Oct 25 2018 09:41
a couple of quick questions about QuerySet.values:
  • is there a "negative" values method? I want to select ALL attributes EXCEPT one. As of now, to do this I have to explicitly indicate the ones I want (say I don't want attr7): queryset.values('attr1', 'attr2', 'attr3', 'attr4', 'attr5', 'attr6'). Isn't there a method like this?: queryset.values_except('attr7').
  • is there a way to automatically populate ForeignKey attributes with the corresponding object instead of its id?
matfire
@matfire
Oct 25 2018 12:09
Hello everyone
I am trying to optimize some queries for data visualization. Currently I have 12k+ queries, because I constantly filter on a bigger queryset. I tried writing a function that could perform the filter operation(as django apparently queries the database for each filter call), but evet though the queries dropped to 7k, loading time has increased drastically. Anyone ever dealt with this problem?
RAHUL KOUNDAL
@rahulkoundal
Oct 25 2018 13:09
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))
please explain this line
i don't know but is the meaning of the line
Jatin Katyal
@jatinkatyal13
Oct 25 2018 13:10
it fetched the root directory of your project on your system
fetches*
RAHUL KOUNDAL
@rahulkoundal
Oct 25 2018 13:12
okay thanks @jatinkatyal13
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
@jatinkatyal13 please tell me the name of the database
i am new in the django
how i find the name of the database
Jatin Katyal
@jatinkatyal13
Oct 25 2018 13:13
'NAME': os.path.join(BASE_DIR, 'db.sqlite3’)
this line uses that absolute path we fetched above to find the filenamed db.sqlite3 in out project root !
if this file doesn’t exists it’ll create one for you as soon as you run
python manage.py migrate
RAHUL KOUNDAL
@rahulkoundal
Oct 25 2018 13:15
@jatinkatyal13 so i am confused whether name of the database is db.sqlite3 ?
Jatin Katyal
@jatinkatyal13
Oct 25 2018 13:16
yes !
RAHUL KOUNDAL
@rahulkoundal
Oct 25 2018 13:16
database name is db.sqlite3
Jatin Katyal
@jatinkatyal13
Oct 25 2018 13:16
basically sqlite is the file based database. unlike mysql and postgresql it doesn’t requires daemon.
RAHUL KOUNDAL
@rahulkoundal
Oct 25 2018 13:17
@jatinkatyal13
what is daemon ??
Jatin Katyal
@jatinkatyal13
Oct 25 2018 13:18
a program that listens for request
like a mysql daemon would listen for requests on 3306 port and will act accordingly
RAHUL KOUNDAL
@rahulkoundal
Oct 25 2018 13:18
okay thankyou
so when i am try the connect to the database
Jatin Katyal
@jatinkatyal13
Oct 25 2018 13:18
but in the case of sqlite all it requires is the file containing the database
in sqlite you don’t exactly connect to the database you just open the file !
RAHUL KOUNDAL
@rahulkoundal
Oct 25 2018 13:20
okay
but i have some problem like if i run the python file directly
because i have read the excel file which contain user creditional and store in the database
so i need to connection to the database
please help me because i have found the diffuclty to do this @jatinkatyal13
Jatin Katyal
@jatinkatyal13
Oct 25 2018 13:25
you can use django shell to read the csv file and create model instances
fsecada01
@fsecada01
Oct 25 2018 13:26

@rahulkoundal you really should follow a tutorial instead of asking people for help with each new error. What you do when starting with Django is the following:

1) Declare the connection properties for your database (SQLite, MySQL, PostgreSQL etc)

2) Connect to your database

3) Make requests to your database to pull or write data

Since you are starting with Django, you should learn how to use the ORM for collecting data from your DB instead of writing the connection data explicitly. This is because Django's native ORM handles a lot of the SQL under the hood and using the ORM provides an additional security layer to protect your database

@matfire

Hello everyone
I am trying to optimize some queries for data visualization. Currently I have 12k+ queries, because I constantly filter on a bigger queryset. I tried writing a function that could perform the filter operation(as django apparently queries the database for each filter call), but evet though the queries dropped to 7k, loading time has increased drastically. Anyone ever dealt with this problem?

You should consider using Q objects. This helped a lot with my own use of querying and running cron jobs.

https://docs.djangoproject.com/en/2.1/topics/db/queries/#complex-lookups-with-q

RAHUL KOUNDAL
@rahulkoundal
Oct 25 2018 13:33
@fsecada01 tutorial going to confuse me
every tutorial have new ways to doing the things
fsecada01
@fsecada01
Oct 25 2018 13:34
@rahulkoundal you have to learn how to follow prescribed steps before you start going off on your own. Stick to ONE tutorial and follow their directions instead of doing a bunch at once. Pick one that at least uses Django 2.0 and Python 3.6. You'll be fine after that.
RAHUL KOUNDAL
@rahulkoundal
Oct 25 2018 13:35
@fsecada01 you know any best tutorial of the django ??
fsecada01
@fsecada01
Oct 25 2018 13:36

literally just pick one from online. There are a bunch on youtube.

Coding for Entrepreneurs has a lot of really good tutorials.

RAHUL KOUNDAL
@rahulkoundal
Oct 25 2018 13:36
okay
Rajesh P
@rajeshcis
Oct 25 2018 14:20
@rahulkoundal you can reat this tutorial to understand the basic flow of django https://simpleisbetterthancomplex.com/series/beginners-guide/1.11/
This tutorial will help you lots
fsecada01
@fsecada01
Oct 25 2018 16:08
This is for Django 1.11, though.
Rajesh P
@rajeshcis
Oct 25 2018 16:08
yes
M Abdullah Abid
@wulaaf
Oct 25 2018 16:16
Videos by sentdex on YouTube are good for beginners
And I agree, coding for entrepreneurs and simpleisbetterthamcomplex is quite nice. On top of everything, Django documentation is your go to guide for everything
It takes a little while to get a hang of it but it's quite comprehensive and has everything you need
It'll take you months maybe even years to understand all of it but you can get started in a day or two
Also, read about MVC, Django template engine, models, views, forms, signals, etc.
Also Django awesome to find curated packages for pretty much everything from analytics to social logins to full fledge erps, crms and cms