pylance-release
pylance-release copied to clipboard
Django. support django orm autocompletion
In pycharm or using the following instruction: described here, - the user can use code completion based on the "related name" value of the model's "foreign key" relasionships.
Can you add this to Pylance, at now this do not work.
That would be awesome!
Do you have an example? This seems to work for me:
from __future__ import annotations
from django.db import models
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from django.db.models import Manager
class Article(models.Model):
tags: Manager["Tag"]
# Declare the ForeignKey with related_name
class Tag(models.Model):
article = models.ForeignKey(
Article,
on_delete=models.CASCADE,
related_name="tags"
)
name = models.CharField(max_length=255)
# Return all tags
Article.tags.all
At least that's what the django issue described as the solution. We do already ship the django_stubs, so it should pick those up automatically.
Gents, there's more that I think can be done.
class Transmission(models.Model):
stations = models.ManyToManyField("Station", through="StationTransmission")
sport = models.ForeignKey("Sport", on_delete=models.PROTECT, null=True, related_name="transmissions")
at = models.DateTimeField(db_index=True, null=False)
class Sport(models.Model):
type = models.TextField(choices=SportType.choices(), db_index=True, default=None)
class StationTransmission(BaseMixin):
transmission = models.ForeignKey("Transmission", related_name="station_transmissions", on_delete=models.CASCADE)
station = models.ForeignKey("Station", on_delete=models.PROTECT)
class Station(BaseMixin):
name = models.TextField()
Field filtering, note type specific completions:
![image](https://user-images.githubusercontent.com/1809191/205354174-8f45d6d0-078b-47cd-96b0-a2184ca2d223.png)
Filtering by foreign_key fields:
![image](https://user-images.githubusercontent.com/1809191/205354236-3a4610ba-c69b-47ac-b0ca-7df9067cf8b3.png)
Filtering by related_name:
![image](https://user-images.githubusercontent.com/1809191/205355442-6f81f275-6e34-46db-8adf-2bf61a04fc32.png)
Double underscore is django orm thing. Having this kind of competition would be luxurious.
Thanks @diego351. Are your screenshots from Pycharm?
@rchiodo yes. The best django integration out there so far I think. I made a switch from pycharm so would like to help to surpass it :)
У вас есть пример? Кажется, это работает для меня:
from __future__ import annotations from django.db import models from typing import TYPE_CHECKING if TYPE_CHECKING: from django.db.models import Manager class Article(models.Model): tags: Manager["Tag"] # Declare the ForeignKey with related_name class Tag(models.Model): article = models.ForeignKey( Article, on_delete=models.CASCADE, related_name="tags" ) name = models.CharField(max_length=255) # Return all tags Article.tags.all
По крайней мере, это то, что проблема django описана как решение. Мы уже отправляем django_stubs, так что они должны автоматически загружаться.
Do you have an example? This seems to work for me:
from __future__ import annotations from django.db import models from typing import TYPE_CHECKING if TYPE_CHECKING: from django.db.models import Manager class Article(models.Model): tags: Manager["Tag"] # Declare the ForeignKey with related_name class Tag(models.Model): article = models.ForeignKey( Article, on_delete=models.CASCADE, related_name="tags" ) name = models.CharField(max_length=255) # Return all tags Article.tags.all
At least that's what the django issue described as the solution. We do already ship the django_stubs, so it should pick those up automatically.
If you do not use an additional parameter declaration wrapped in a manager in the class, then there is no access to the auto-completion of the related_name field
Can you give a concrete example? Do you mean this class does not have the 'Manager' type on it?
class Article(models.Model):
tags: Manager["Tag"]
If I don't create "tag: Manager["Tag"]" , I won't have access to the "related_name="tags"" field elsewhere in the code for autocompletion. Unlike pycharm. The author of the "crutch" writes about this on the forum 'django'.
Do you have a link to the forum post? I'd love to read what they're talking about. Not having much luck searching for it.
Do you have a link to the forum post? I'd love to read what they're talking about. Not having much luck searching for it.
https://django.fun/en/qa/418209/
class Transmission(models.Model):
stations = models.ManyToManyField("Station", through="StationTransmission")
sport = models.ForeignKey("Sport", on_delete=models.PROTECT, null=True, related_name="transmissions")
at = models.DateTimeField(db_index=True, null=False)
class Sport(models.Model):
type = models.TextField(choices=SportType.choices(), db_index=True, default=None)
class StationTransmission(BaseMixin):
transmission = models.ForeignKey("Transmission", related_name="station_transmissions", on_delete=models.CASCADE)
station = models.ForeignKey("Station", on_delete=models.PROTECT)
class Station(BaseMixin):
name = models.TextField()
.prefetch_related() and .select_related() completions
![image](https://user-images.githubusercontent.com/1809191/205440469-1bef50a6-ec16-49a4-b4e4-42a2c4a04864.png)
![image](https://user-images.githubusercontent.com/1809191/205440518-73ab8e21-3803-4a4c-abf4-541b8b82d6e3.png)
Please note ForeignKey reference by string, defined in the same file but further on.
@rchiodo Hi there. Can I ask if there's any progress on this? Also, changing name of this issue to "support django orm autocompletion" might be good idea. Thanks!
Thanks for the feedback. Unfortunately, this is still pretty far down the priority list. We've been discussing some things related to this: https://viicos.github.io/posts/an-alternative-to-the-django-mypy-plugin/, but haven't implemented anything yet.
we could do similar thing we did for pytest completion
(transforming original text to contains extra type annotation behind the scene) but unlike pytest completion
, we would need to do that for workspace
wide not just 1 document, since source of the trigger (where related_tag is added) and destination of the effect (where completion is shown) are different.